我想用Presto SQL编写一个查询。
表格:
words | id1 | id2 | id2_like排名||
---|---|---|---|---|
棒球 | 28 | 2756 | 1||
棒球 | 28 | 3180。 | 0。5 | |
棒球 | 28。 | 8161。 | 0。 | >17 |
棒球 | 11。 | 1723 | 0。 | 22 |
棒球 | 11. | <29>|||
足球 | 19。 | 3210。 | 1. | 2 |
足球 | 19。 | 5519 | 0。 | 18 |
足球 | 19。 | 6257 | <1。>3 |
希望它能帮助你了解该做什么,并在AWS Athena中进行测试(很像在引擎盖下的presto(。不理解第五个问题。
SELECT
words,
item_1,
item_1 / CAST(size as decimal(10,4)) * 100 as item_2,
size - item_1 as item_3,
max_rank as item_4
FROM (
SELECT
words,
SUM(id2_like) as item_1,
COUNT(*) as size,
AVG(id1/CAST((SELECT MAX(rank) FROM tb WHERE id2_like = 0) as decimal(10,4))) as max_rank
FROM tb
GROUP BY 1
)
如果我理解正确,这里是你想要的,但我不理解你想要的5号是什么
select words
, sum(id1_cnt_for_id2_as_1) as id1_cnt_for_id2_as_1
, sum(sum_perc_id2_as_0)* 100.0 /sum(cnt_perc_id2_as_0) as perc_id2_as_0
, sum(id1_cnt_for_id2_as_0_perc) id1_cnt_for_id2_as_0_perc
, avg(max_rank_id2_as_0) as max_rank_id2_as_0
, avg(avg_perc_id2_as_0) as avg_perc_id2_as_0
from (
select words
, sum(id2_like) as id1_cnt_for_id2_as_1
, sum(case when id2_like= 0 then 1 end) as sum_perc_id2_as_0
, count(*) as cnt_perc_id2_as_0
, count(distinct case when id2_like =0 then id1 end) id1_cnt_for_id2_as_0_perc
, sum(case when id2_like= 0 then rank end) as max_rank_id2_as_0
, sum(case when id2_like= 0 then 1 end)* 100.0/count(*) as avg_perc_id2_as_0
from data
group by words,id1
) t group by words
db<gt;小提琴这里