sql - MySQL user activity across time starting with last week -


i have user activity data

 datetime | user | action --------------------------     t1    |  u1  |       t2    |  u1  |   b     t3    |  u2  |       t4    |  u1  |   c     t1    |  u2  |       t1    |  u2  |   d 

aggregated output should follows

t1    |  u1  |   1 t1    |  u2  |   2 .... 

if today july 22,2015 need create buckets following

  1. jul 15,2015 12 noon - jul 16,2015 12 noon , corresponding counts

  2. jul 16,2015 12 noon - jul 17,2015 12 noon

  3. jul 17,2015 12 noon - jul 18,2015 12 noon
  4. jul 18,2015 12 noon - jul 19,2015 12 noon....

how can that?

if i'm understanding question correctly trying count of user actions per day.

to accomplish going use group in conjunction aggregate function: count

select date("datetime"), "user", count("action") tablename  date("datetime") > curdate() group date("datetime"), user 

if want offset 12 hours you'll need som date math. date function docs

writing phone may have clean up, replacing every date("datetime") along lines of following should work

case      when `datetime` between date_add(cur_date(), interval -7 -12 day_hour) , date_add(cur_date(), interval -6 -12 day_hour) '7 days ago'     when `datetime` between date_add(cur_date(), interval -6 -12 day_hour) , date_add(cur_date(), interval -5 -12 day_hour) '6 days ago'     .... end  

there's more elegant/efficient solutions, should going in right direction


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -