SQL - Unequal left join BigQuery

后端 未结 1 1086
梦谈多话
梦谈多话 2020-12-21 23:36

New here. I am trying to get the Daily and Weekly active users over time. they have 30 days before they are considered inactive. My goal is to create graph\'s that can be sp

相关标签:
1条回答
  • 2020-12-21 23:54

    Below is for BigQuery Standard SQL and mostly reproduce logic in your query with exception of not including days where no activity at all is found

    #standardSQL
    SELECT
        daily_use.user_id
      , wd.date AS DATE
      , MIN(DATE_DIFF(wd.date, daily_use.activity_date, DAY)) AS days_since_last_action
    FROM dates AS wd
    CROSS JOIN daily_use
    WHERE wd.date BETWEEN 
      daily_use.activity_date AND DATE_ADD(daily_use.activity_date, INTERVAL 30 DAY)
    GROUP BY 1,2
    -- ORDER BY 1,2
    

    if for whatever reason you still need to exactly reproduce your logic - you can embrace above with final left join as below:

    #standardSQL
    SELECT *
    FROM dates AS wd
    LEFT JOIN (
      SELECT
        daily_use.user_id
        , wd.date AS date
        , MIN(DATE_DIFF(wd.date, daily_use.activity_date, DAY)) AS days_since_last_action
      FROM dates AS wd
      CROSS JOIN daily_use
      WHERE wd.date BETWEEN 
        daily_use.activity_date AND DATE_ADD(daily_use.activity_date, INTERVAL 30 DAY)
      GROUP BY 1,2
    ) AS daily_use
    USING (date)
    -- ORDER BY 1,2
    
    0 讨论(0)
提交回复
热议问题