MySQL Count multiple distinct records with condition -
i have table records user cross-profile visits , need count of distinct visits , count of new visits specified profile (:user_id) since specified unix timestamp (:time).
id | user_id | visitor_id | time (unix) ====+==========+============+============= 1 | 1 | 5 | 1300000000 2 | 1 | 5 | 1300000010 3 | 1 | 8 | 1300000015 4 | 1 | 9 | 1300000020 5 | 1 | 9 | 1300000025 6 | 1 | 9 | 1300000030
so far able count of total visits, unable new ones.
select count(distinct v.visitor_id) total, sum(v.time > 1300000012) new profile_visits v v.user_id = 1
returns
total | new ============ 3 | 4
but required result is
total | new ============ 3 | 2
you want sum(v.time > 1300000012) new
.
the expression either 0 or 1. count()
count 0 happily 1. sum()
"count" 1's.
re comment:
select count(t.visitor_id) total, sum(t.subtotal_new) total_new (select v.visitor_id, sum(v.time > 1300000012) subtotal_new profile_visits v group v.visitor_id) t
the sum()
of inner query counts new visits per visitor. sum()
of outer query gives total new visits for visitors.
Comments
Post a Comment