什么是执行此查询的最佳方法(SQL)

时间:2020-04-09 13:56:41

标签: sql snowflake-cloud-data-platform

我需要进行查询,但是我找不到有效的方法。

客户每次听一首歌,就会用他的键和track_id,日期...记录一行。

我想检查每个客户,如果他上个月收听的曲目是新曲目(本月之前从未收听过)

一行如下:

key | track_id | date
asd | 12312    | 12/02/2020
fds | 12323    | 12/05/2020

我认为我可以使用窗口函数执行某些操作,但似乎找不到一种好的方法。

然后,我还需要从此列表中获得前3名听众最多的歌曲,我可以通过窗口功能来实现。

如果有人可以帮助我?非常感谢。

2 个答案:

答案 0 :(得分:1)

使用聚合和窗口功能:

select key, count(*) as num_rows,
       count(*) as num_tracks,
       sum( case when first_yyyymm = yyyymm then 1 else 0 end) as num_new_tracks,
       sum( case when first_yyyymm < yyyymm then 1 else 0 end) as num_prev_tracks
from (select t.key, track_id, date_trunc('month', date) as yyyymm,
             min(date_trunc('month', date) ) over (partition by key, track_id) as first_yyyymm
      from t
      group by key, track_id, yyyymm
     ) t
where yyyymm >= date_trunc('month', current_date)
group by key

答案 1 :(得分:0)

一条建议。如果您需要别人的帮助,请始终在示例数据集中包含DDL。期望人们为您做这是不公平的。 (在这种情况下,我做到了)

create table sandbox.songs 
(key varchar,
track_id number,
dt date);

insert into  sandbox.songs 
values
('asd', 12312, '12/02/2020'),
('fds', 12323, '12/05/2020'),
('asd', 11000, '04/05/2020'),
('fds', 92823, '04/07/2020'),
('asd', 11000, '3/05/2020'),
('fds', 12323, '12/05/2020'),
('asd', 92834, '3/05/2020');

问题1

select 
    key, 
    count(0), 
    sum(case when dt < current_date - 30 then 1 else 0 end) as older_than_30,
    sum(case when dt >= current_date - 30 then 1 else 0 end) as within_30    
from sandbox.songs group by key;

[在此处输入图片描述] [1]

问题2

track_id, 
count(0) 
from sandbox.songs 
group by track_id 
order by count(0) desc 
limit 3;

[enter image description here][2]


  [1]: https://i.stack.imgur.com/CeDqw.png
  [2]: https://i.stack.imgur.com/sdKV0.png
相关问题