简体   繁体   中英

Getting unique users for consecutive days

I have a task to find unique users from our table for cumilative days.

What I wanted to get is

select count(distinct(userid))  from session_tab where 
date(session) = '2016-01-15 00:00:00'
union all
select count(distinct(userid))  from session_tab where 
date(session in ('2016-01-15 00:00:00','2016-01-16 00:00:00')
union all
select count(distinct(userid))  from session_tab where 
date(session in ('2016-01-15 00:00:00','2016-01-16 00:00:00','2016-01-16 00:00:00')

and so on...for next 30 days.

Can some one pls let me know how can this be achieved

To get the next 30 days, go between CURDATE() and CURDATE() plus 30 days. After that, it's just a case of counting and grouping:

SELECT DATE(session), COUNT(DISTINCT userid)
  FROM session_tab
  WHERE DATE(session) BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 30 DAY)
  GROUP BY DATE(session)
  ORDER BY DATE(session);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM