提问人:RustyShackleford 提问时间:5/23/2022 更新时间:7/8/2022 访问量:90
如何比较一行中的一个值,看看它是否高于同一列中所有值的 75%?
How to compare one value in a row to see if it is higher than 75% of all values in the same column?
问:
我有一个表格,如下所示:
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%,以创建新的标志列?ratio
greater_75p
新表应如下所示(无法格式化新列,但应作为选项):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)
我尝试了这个查询,但得到错误: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
不确定我做错了什么以及如何在 sql 中得出百分比的比较?
先谢谢你。
答:
1赞
Shuvo
5/23/2022
#1
要获得结果,您可以使用公共表表达式,如下所示:percent_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
请澄清计算列的逻辑。greater_75p
评论
0赞
RustyShackleford
5/23/2022
感谢您的回复!这看起来很有希望。的逻辑是,如果该比率高于所有其他比率的 75%,则将“y”标志添加到新列中。greater_75p
0赞
Shuvo
5/23/2022
@RustyShackleford 我已经根据您的澄清修改了我的答案。希望这是你现在想要的答案。
0赞
RustyShackleford
5/23/2022
这看起来不错。出于好奇,你认为分位数会是一个更好的选择吗?hevodata.com/learn/bigquery-quantiles/#6
0赞
Shuvo
5/23/2022
我不确定@RustyShackleford。但我更喜欢percent_rank()。
评论