最近6个月每月听超过10分钟的人

时间:2018-12-14 17:54:30

标签: sql google-bigquery

我正在尝试计算过去6个月每月收听超过10分钟的用户的数量

我们有一个事件:Song_stopped_listen,一个属性是session_progress_ms

现在,我正在尝试查看过去6个月中该队列人数的每月变化。

我正在使用bigquery,这是我尝试过的查询,但是我觉得在语义上有些不对劲,但是我无法动手:

SELECT 
CONCAT(CAST(EXTRACT(YEAR FROM DATE (timestamp)) AS STRING),"-",CAST(EXTRACT(MONTH FROM DATE (timestamp)) AS STRING)) AS date
,SUM(absl.session_progress_ms/(1000*60*10)) as total_10_ms, COUNT(DISTINCT u.id) as total_10_listeners
FROM ios.song_stopped_listen as absl
LEFT JOIN ios.users u on absl.user_id = u.id 
WHERE absl.timestamp > '2018-05-01'
Group by 1 
HAVING(total_10_ms > 1) 

请帮助找出我在这里做错了什么。 谢谢。

数据样本:

user_id | session_progress_ms | timestamp
1       |         10000       |    2017-10-10 14:34:25.656 UTC

我想要拥有的东西:

||Month-year | Count of users who listened to more than 10 mins
|2018-5     | 500
|2018-6     | 600
|2018-7     | 300
|2018-8     | 5100
|2018-9     | 4500
|2018-10    | 1500
|2018-11    | 1500
|2018-12    | 2500

1 个答案:

答案 0 :(得分:1)

使用多个级别的聚合:

select user_id
from (select ssl.user_id, timestamp_trunc(timestamp, month) as mon,
             sum(ssl.session_progress_ms/(1000*60)) as total_minutes
      from ios.song_stopped_listen as ssl 
      where date(ssl.timetamp) < date_trunc(current_date, month) and
            date(ssl.timestamp) >= date_add(date_trunc(current_date, month) interval 6 month), 
      group by 1, 2 
     ) u
where total_minutes >= 10 
group by user_id
having count(*) = 6;

要获取计数,只需将其用作count(*)的子查询即可。