多天的条件汇总

时间:2019-07-17 21:58:41

标签: sql hiveql

我可以使用下面的SQL进行一天的条件聚合,但是想知道如何在一个查询中连续几天完成它。我正在尝试在logs_20190715和日期之间做笛卡尔积,但无法考虑进一步解决这个问题。任何输入将不胜感激。

--1
    SELECT CAST('2018-11-19' AS TIMESTAMP ) AS time_id,
                   city_id,
                   COUNT( DISTINCT CASE WHEN DATE_TRUNC('DAY',logged_at) = CAST( '2018-11-19' AS TIMESTAMP ) THEN user_id END ) AS A,
                   COUNT( DISTINCT CASE WHEN logged_at >= CAST( '2018-11-19' AS TIMESTAMP )  
                                         AND logged_at < CAST( '2018-11-19' AS TIMESTAMP ) + interval '7' DAY 
                                        THEN user_id 
                                    END
                         ) AS B,
                   COUNT( DISTINCT CASE WHEN logged_at < CAST( '2018-11-19' AS TIMESTAMP )  
                                         AND logged_at >= CAST( '2018-11-19' AS TIMESTAMP ) - interval '7' DAY 
                                        THEN user_id 
                                    END
                         ) AS C,
                   COUNT( DISTINCT CASE WHEN logged_at < CAST( '2018-11-19' AS TIMESTAMP )  
                                         AND logged_at >= CAST( '2018-11-19' AS TIMESTAMP ) - interval '28' DAY 
                                        THEN user_id 
                                    END
                         ) AS D,  
                   '2018-11-19' 
              FROM logs_20190715
             WHERE logged_at <= CAST('2018-11-19' AS TIMESTAMP) + interval '10' DAY
               AND logged_at >= CAST('2018-11-19' AS TIMESTAMP) - interval '40' DAY
            GROUP BY 1,2;


--2
SELECT CAST('2018-11-18' AS TIMESTAMP ) AS time_id,
                   city_id,
                   COUNT( DISTINCT CASE WHEN DATE_TRUNC('DAY',logged_at) = CAST( '2018-11-18' AS TIMESTAMP ) THEN user_id END ) AS A,
                   COUNT( DISTINCT CASE WHEN logged_at >= CAST( '2018-11-18' AS TIMESTAMP )  
                                         AND logged_at < CAST( '2018-11-18' AS TIMESTAMP ) + interval '7' DAY 
                                        THEN user_id 
                                    END
                         ) AS B,
                   COUNT( DISTINCT CASE WHEN logged_at < CAST( '2018-11-18' AS TIMESTAMP )  
                                         AND logged_at >= CAST( '2018-11-18' AS TIMESTAMP ) - interval '7' DAY 
                                        THEN user_id 
                                    END
                         ) AS C,
                   COUNT( DISTINCT CASE WHEN logged_at < CAST( '2018-11-18' AS TIMESTAMP )  
                                         AND logged_at >= CAST( '2018-11-18' AS TIMESTAMP ) - interval '28' DAY 
                                        THEN user_id 
                                    END
                         ) AS D,  
                   '2018-11-18' 
              FROM logs_20190715
             WHERE logged_at <= CAST('2018-11-18' AS TIMESTAMP) + interval '10' DAY
               AND logged_at >= CAST('2018-11-18' AS TIMESTAMP) - interval '40' DAY
            GROUP BY 1,2;

如何将以上两个查询合并为一个查询,并产生相同的结果?(具有包含所有日期的date维度。)

0 个答案:

没有答案