使用SQL选择唯一ID的事件日期之间的差异

时间:2018-06-06 17:51:17

标签: sql count min datediff

我必须计算每个用户每天执行的事件数量。为此,我必须连接两个表,其中一个表包含user_id和每个事件的日期,另一个表包含每个日期的日历。

我尝试了第一个唯一用户ID,但 delta_day 的输出为0:

 with a1 as (SELECT DISTINCT buyer_account_id_fk,
            event_date,
            count(event_date) as qtide,
            min(event_date) as first_event
 FROM ods.ad_contacts
 WHERE buyer_account_id_fk = 14954020
 and year = 2018
 GROUP BY 1,2
 LIMIT 1000),
 a2 as (select distinct buyer_account_id_fk,event_dt, qtide, 
 date_diff('day', first_event,event_dt) AS delta_day
 from a1
 RIGHT JOIN ods.calendar AS cal ON a1.event_date = cal.event_dt
 limit 1000)
 select buyer_account_id_fk, delta_day, qtide
 from a2
 group by 1,2,3


BUYER_ACCOUNT_ID_FK DELTA_DAY   QTIDE
    14,954,020         0         15
    14,954,020         0         8
    14,954,020         0         1
    14,954,020         0         22
    14,954,020         0         5
    14,954,020         0         9

我意识到问题在于确定 min(event_date) ,其中对于相同的user_id,结果与 event_date相同 ,因为 delta_day 是igual为0.但我不知道如何解决这个问题。

拜托,帮助我!!

表: ods.ad_contacts

BUYER_ACCOUNT_ID_FK EVENT_DATE
14,954,020            03/01/18
14,954,020            06/01/18
14,954,020            06/01/18
14,954,020            08/01/18

表: ods.calendar

EVENT_DT
01/01/18
02/01/18
03/01/18
04/01/18
05/01/18
06/01/18
07/01/18
08/01/18

我希望结果如下:

buyer_account_id_fk  delta_day  qtide   
  14,954,020           0          1
  14,954,020           3          2
  14,954,020           5          1

0 个答案:

没有答案