如何比较一行中的一个值,看它是否高于同一列中所有值的 75%?
How to compare one value in a row to see if it is higher than 75% of all values in the same column?
我有一个 table 看起来像这样:
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;
如何逐行比较 ratio
中的每个值以查看该比率是否大于所有比率的 75% 以创建新的标志列,greater_75p
?
新的 table 应该是这样的(无法格式化新的列,但应该是 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 内的百分比比较?
提前致谢。
要获得 percent_rank() 的结果,您可以使用如下常见的 table 表达式:
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
列的逻辑。
我有一个 table 看起来像这样:
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;
如何逐行比较 ratio
中的每个值以查看该比率是否大于所有比率的 75% 以创建新的标志列,greater_75p
?
新的 table 应该是这样的(无法格式化新的列,但应该是 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 内的百分比比较?
提前致谢。
要获得 percent_rank() 的结果,您可以使用如下常见的 table 表达式:
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
列的逻辑。