如何连续比较一个值以查看它是否高于同一列中所有值的75%?

发布于 2025-01-31 03:05:28 字数 1303 浏览 3 评论 0原文

我有一个看起来像这样的表:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒与心事 2025-02-07 03:05:28

要获取persecon_rank()的结果,您可以使用以下通用表表达式:

with cte as
(SELECT *, ups  / SUM(ups) OVER () AS ratio
FROM table) 
select *,(case when percent_rank()over(order by ration) >0.75 then 'yes' else 'no' end) greater_75p from cte

请阐明计算greate_75p列的逻辑。

To get result of percent_rank() you can use common table expression as below:

with cte as
(SELECT *, ups  / SUM(ups) OVER () AS ratio
FROM table) 
select *,(case when percent_rank()over(order by ration) >0.75 then 'yes' else 'no' end) greater_75p from cte

Please clarify the logic for calculating greater_75p column.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文