2017-06-16 64 views
0

需要计数的用户的数量,一个例如如下所示:SQL:报错从日期和日期的日期范围,每天加入查询用于获取日期明智增量

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' 

的输出将是像:

reportDate | count 
------------+------- 
2017-06-01 | 2467 
2017-06-02 | 2470 

所以,我只是有没有fromdate和todate和我需要在表格日期明智不同的用户数。我不会使用任何程序或循环。

回答

1
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) 
+0

这将给每一天不同的用户的数量,需要的是没有总不同的用户的@lowerBoundDate前,然后每一天的用户数被添加到下一个日期。 –

0

要获得累积(不同)用户每天计数,请使用以下内容,将以下示例中给出的自定义日期替换为开始日期和结束日期。

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 
+0

这将给出cumulative_user_count相对于前一天的计数,例如: date | distinct_user_per_day | cumulative_user_count ------------ + ----------------------- + ----------- ------------ 2017-06-01 | 385 | 385 2017-06-02 | 372 | 757 因此,我们将前一天的用户数添加到当天的用户数,实际上大多数用户在两天内都很常见,并且所需的是包括前两天在内的全部唯一用户。 –