SQL:获取按日期递增的查询

SQL:获取按日期递增的查询,sql,analytics,vertica,Sql,Analytics,Vertica,需要计算每天添加的用户数,给定从日期到日期的日期范围,例如如下所示: select '2017-06-01' as myDate , count(distinct user_id) from tbl_stats where date(dateTime)<='2017-06-01' union all select '2017-06-02' as myDate , count(distinct user_id) from tbl_stats where date(dat

需要计算每天添加的用户数,给定从日期到日期的日期范围,例如如下所示:

select 
  '2017-06-01' as myDate
, count(distinct user_id) 
from tbl_stats 
where date(dateTime)<='2017-06-01' 
union all 
select
  '2017-06-02' as myDate
, count(distinct user_id) 
from tbl_stats 
where date(dateTime)<='2017-06-02'

所以,我只需要fromDate和toDate,我需要表中的日期不同的用户计数。我不会使用任何过程或循环。

若要获取每天累积的不同用户数,请使用以下命令,将以下示例中给出的自定义日期替换为开始日期和结束日期

SELECT DATE(ts.dateTime) AS reportDate
    , COUNT(distinct ts.user_id) AS userCount
FROM tbl_stats AS ts
WHERE ts.dateTime >= @lowerBoundDate 
    AND ts.dateTime < TIMESTAMPADD('DAY', 1, @upperBoundDate)
GROUP BY DATE(ts.dateTime)
WITH test_data AS (
        SELECT '2017-01-01'::date as event_date, 1::int as user_id
        UNION
        SELECT '2017-01-01'::date as event_date, 2::int as user_id
        UNION
        SELECT '2017-01-02'::date as event_date, 1::int as user_id
        UNION
        SELECT '2017-01-02'::date as event_date, 2::int as user_id
        UNION
        SELECT '2017-01-02'::date as event_date, 3::int as user_id
        UNION
        SELECT '2017-01-03'::date as event_date, 4::int as user_id
        UNION
        SELECT '2017-01-03'::date as event_date, 5::int as user_id
        UNION
        SELECT '2017-01-04'::date as event_date, 1::int as user_id
        UNION
        SELECT '2017-01-04'::date as event_date, 2::int as user_id
        UNION
        SELECT '2017-01-04'::date as event_date, 3::int as user_id
        UNION
        SELECT '2017-01-04'::date as event_date, 4::int as user_id
        UNION
        SELECT '2017-01-04'::date as event_date, 5::int as user_id
        UNION
        SELECT '2017-01-04'::date as event_date, 6::int as user_id
        UNION
        SELECT '2017-01-05'::date as event_date, 3::int as user_id
        UNION
        SELECT '2017-01-05'::date as event_date, 4::int as user_id
        UNION
        SELECT '2017-01-05'::date as event_date, 5::int as user_id
        UNION
        SELECT '2017-01-05'::date as event_date, 6::int as user_id
        UNION
        SELECT '2017-01-05'::date as event_date, 7::int as user_id
        UNION
        SELECT '2017-01-05'::date as event_date, 8::int as user_id
        UNION
        SELECT '2017-01-06'::date as event_date, 7::int as user_id
        UNION
        SELECT '2017-01-06'::date as event_date, 9::int as user_id
)
SELECT event_date,
       COUNT(distinct user_id) AS distinct_user_per_day,
       SUM(COUNT(distinct user_id)) OVER (ORDER BY event_date) AS cumulative_user_count
FROM test_data
WHERE event_date >= '2017-01-01' 
      AND 
      event_date <= '2017-01-06'
GROUP BY 
      event_date

这将给出每天不同用户的计数,所需为@lowerBoundDate之前的不同用户总数,然后每天将用户数相加到下一个日期。这将给出与前一天计数相关的累计用户数,如:日期-每一天的不同用户数-累计用户数------++------------------2017-06-01 | 385 | 385 2017-06-02 | 372 |757所以我们将前一天的用户数加上当前一天的用户数,实际上大多数用户在这两天都是普通用户,需要的是包括前两天在内的所有唯一用户总数。
WITH test_data AS (
        SELECT '2017-01-01'::date as event_date, 1::int as user_id
        UNION
        SELECT '2017-01-01'::date as event_date, 2::int as user_id
        UNION
        SELECT '2017-01-02'::date as event_date, 1::int as user_id
        UNION
        SELECT '2017-01-02'::date as event_date, 2::int as user_id
        UNION
        SELECT '2017-01-02'::date as event_date, 3::int as user_id
        UNION
        SELECT '2017-01-03'::date as event_date, 4::int as user_id
        UNION
        SELECT '2017-01-03'::date as event_date, 5::int as user_id
        UNION
        SELECT '2017-01-04'::date as event_date, 1::int as user_id
        UNION
        SELECT '2017-01-04'::date as event_date, 2::int as user_id
        UNION
        SELECT '2017-01-04'::date as event_date, 3::int as user_id
        UNION
        SELECT '2017-01-04'::date as event_date, 4::int as user_id
        UNION
        SELECT '2017-01-04'::date as event_date, 5::int as user_id
        UNION
        SELECT '2017-01-04'::date as event_date, 6::int as user_id
        UNION
        SELECT '2017-01-05'::date as event_date, 3::int as user_id
        UNION
        SELECT '2017-01-05'::date as event_date, 4::int as user_id
        UNION
        SELECT '2017-01-05'::date as event_date, 5::int as user_id
        UNION
        SELECT '2017-01-05'::date as event_date, 6::int as user_id
        UNION
        SELECT '2017-01-05'::date as event_date, 7::int as user_id
        UNION
        SELECT '2017-01-05'::date as event_date, 8::int as user_id
        UNION
        SELECT '2017-01-06'::date as event_date, 7::int as user_id
        UNION
        SELECT '2017-01-06'::date as event_date, 9::int as user_id
)
SELECT event_date,
       COUNT(distinct user_id) AS distinct_user_per_day,
       SUM(COUNT(distinct user_id)) OVER (ORDER BY event_date) AS cumulative_user_count
FROM test_data
WHERE event_date >= '2017-01-01' 
      AND 
      event_date <= '2017-01-06'
GROUP BY 
      event_date