目标:查找过去3个月内前200首曲目。 每个轨道的消费者数据为每日。
我用过
RANK () OVER(PARTITION BY report_date ORDER BY SUM(num_streams))
我担心的是,这将使我每天返回前200名不同和我不想要。
我希望前200名每天保持不变。 但是每天都有不同的消费者数据。
有什么主意吗?
我正在使用AWS REDSHIFT
我想要这个:
Day Track_Title Consumer_counts Rank
Jul 1 Halo 600 1
Jul 1 old town road 200 2
Jul 1 heartbeat 180 3
Jul 2 Halo 500 1
Jul 2 old town road 300 2
Jul 2 heartbeat 400 3
但是我明白了
Day Track_Title Consumer_counts Rank
Jul 1 Halo 600 1
Jul 1 old town road 200 2
Jul 1 heartbeat 180 3
Jul 2 Halo 500 1
Jul 2 wish you were here 400 2
Jul 2 old town road 300 3
答案 0 :(得分:0)
如果您希望在3个月内排名前200位的曲目,然后获得仅包含所选200条曲目的每日计数,则可以执行以下操作:
SELECT top 200 --To get the top 200 tracks over 3 months
t.track,
RANK () OVER(PARTITION BY t.track ORDER BY SUM(t.num_streams))
INTO #tempTable
FROM
(SELECT track, num_streams
FROM MyTable
WHERE report_date BETWEEN "2019-01-01" and "2019-04-01") t
SELECT --To get the top 200 tracks for the selected date.
mt.day,
mt.track,
SUM(mt.num_streams)
RANK () OVER(PARTITION BY mt.report_date ORDER BY SUM(mt.num_streams))
FROM #tempTable tt
INNER JOIN MyTable mt on tt.track = mt.track
答案 1 :(得分:0)
我认为您想要一个聚合查询
select Track_Title, sum(Consumer_counts) as total
rank() over (order by sum(Consumer_counts) desc) as ranking
from t
where report_date >= current_date - interval '3 month'
group by Track_Title
order by total desc
limit 200;
如果您想按日查看详细信息,那么我建议重新加入:
with top200 as (
select Track_Title, sum(Consumer_counts) as total
rank() over (order by sum(Consumer_counts) desc) as ranking
from t
where report_date >= current_date - interval '3 month'
group by Track_Title
order by total desc
limit 200
)
select t.*, top200.ranking
from t join
top200
on t.Track_Title = top200.Track_Title
order by t.report_date, top200.ranking;