sql - MySQL Count if the value has not appeared before -
here table of users, carts, , time. user can have multiple carts. anonymous users have userid = 1000; identified user has id different 1000.
all cartids unique.
+------------+-------------+----------------------+ | userid | cartid | time | +------------+-------------+----------------------+ | 7650 | 231 | 2014-08-27 13:41:02 | +------------+-------------+----------------------+ | 7632 | 221 | 2014-08-27 13:42:02 | +------------+-------------+----------------------+ | 7650 | 289 | 2014-08-27 14:13:02 | +------------+-------------+----------------------+ | 1000 | 321 | 2014-08-27 14:41:02 | +------------+-------------+----------------------+ | 7650 | 500 | 2014-08-27 17:41:02 | i interested in calculating number of distinct identified users hour of day.
i tried following, cannot keep record of ids entered before when group them hour(date).
count( distinct (case when userid <> 1000 userid end)) numselfidusers for output, want like:
+------------+-------------+----------------------+ | date | hourofday | numselfidusers | +------------+-------------+----------------------+ | 2014-08-27 | 13 | 2 | +------------+-------------+----------------------+ | 2014-08-27 | 14 | 0 | +------------+-------------+----------------------+ | 2014-08-27 | 17 | 0 | +------------+-------------+----------------------+ please let me know if there questions. in advance help.
i think want this:
select date(time), hour(time), count(distinct case when userid <> 1000 userid end) numselfidusers usercarts date(time) = '2014-08-27' group date(time), hour(time) order 1, 2; this looks similar have in query. i'm not sure why version wouldn't work.
edit:
you seem want times 0 counts. bit more challenging, can this:
select d.dt, h.hr, count(distinct case when userid <> 1000 userid end) (select distinct date(time) dt usercarts dt in range) d cross join (select 0 hr union select 1 union select 2 union select 3 union . . . select 23 ) h left join usercart uc on date(uc.time) = d.dt , hour(uc.time) = h.hr; the . . . put in rest of numbers 3 23.
edit ii:
i suspect looking first time user appears. if so, try this:
select date(firsttime), hour(firsttime), count(*) numfirstusers (select userid, min(time) firsttime usercarts userid <> 1000 group userid ) u group date(firsttime), hour(firsttime) order 1, 2;
Comments
Post a Comment