如何连续比较一个值以查看它是否高于同一列中所有值的75%?
我有一个看起来像这样的表:
groups created_utc score count_comments d_posts ups downs ratio
group1 2011-07-11T19:05:19Z 6988 3742 56 8530 1572 .42(8530/20286)
group2 2011-04-23T21:29:12Z 10455 4695 512 11756 1303 .58(11756/20286)
从此查询中生成:
SELECT *, ups / SUM(ups) OVER () AS ratio
FROM table
order by ratio desc;
如何在比率
逐行比较每个值,以查看该比率是否大于所有比率的75%以创建新的比率标志列,greate_75p
?
新表格应该看起来像这样(无法获得新的col格式化,但应为y/n
作为选项):
groups created_utc score count_comments d_posts ups downs ratio greater_75p
y
group1 2011-07-11T19:05:19Z 6988 3742 56 8530 1572 .42(8530/20286)
group2 2011-04-23T21:29:12Z 10455 4695 512 11756 1303 .58(11756/20286)
我尝试了此查询,但是获取错误标量子查询产生了多个元素< /code>:
SELECT *,ups * 100 / SUM(ups) OVER () AS ratio,
PERCENT_RANK() OVER(order by (SELECT ups * 100 / SUM(ups) OVER () AS ratio from table )) AS greater_75p
FROM table
不确定我在做什么错以及如何得出SQL内的百分比比较?
先感谢您。
I have a table that looks like this:
groups created_utc score count_comments d_posts ups downs ratio
group1 2011-07-11T19:05:19Z 6988 3742 56 8530 1572 .42(8530/20286)
group2 2011-04-23T21:29:12Z 10455 4695 512 11756 1303 .58(11756/20286)
Generated from this query:
SELECT *, ups / SUM(ups) OVER () AS ratio
FROM table
order by ratio desc;
How do I compare each value in ratio
column by row to see if that ratio is greater than 75% of all the ratios to create a new flag column, greater_75p
?
The new table should look like this (cant get the new col formatted but should be y/n
as options):
groups created_utc score count_comments d_posts ups downs ratio greater_75p
y
group1 2011-07-11T19:05:19Z 6988 3742 56 8530 1572 .42(8530/20286)
group2 2011-04-23T21:29:12Z 10455 4695 512 11756 1303 .58(11756/20286)
I tried this query, but get error Scalar subquery produced more than one element
:
SELECT *,ups * 100 / SUM(ups) OVER () AS ratio,
PERCENT_RANK() OVER(order by (SELECT ups * 100 / SUM(ups) OVER () AS ratio from table )) AS greater_75p
FROM table
Not sure what I am doing wrong and how to derive a comparison in percentages within sql?
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要获取
persecon_rank()
的结果,您可以使用以下通用表表达式:请阐明计算
greate_75p
列的逻辑。To get result of
percent_rank()
you can use common table expression as below:Please clarify the logic for calculating
greater_75p
column.