mysql - Multiple distinct counts with where -


i having issue creating efficient query multiple distinct counts of column different clauses. mysql table looks this:

id    client_id    result       timestamp ---------------------------------------------------  1    1234566      escalated    2014-01-02 00:00:00  2    1233344      approved     2014-02-03 00:00:00  3    1234566      escalated    2014-01-02 01:00:00 

what trying achieve build following data in return:

total number of unique client ids processed beginning of time.

total number of unique client ids processed escalated beginning of time.

total number of unique client ids processed approved beginning of time.

count of unique client ids approved within specified timeframe using between statement on timestamp.

count of unique client ids escalated within specified timeframe using between statement on timestamp.

i have thought running multiple selects, think waste of resources, , possibly if done single query best way handle it, unfortunately experience lacking in area. return simple contain alias , count.

any appreciated.

you want conditional aggregation, like:

select count(distinct clientid) numclients,        count(distinct case when result = 'approved' clientid end) numapproved,        count(distinct case when result = 'escalated' clientid end) numescalated,        count(distinct case when result = 'approved' , timestamp between @time1 , @time2                             clientid end) numapproved,        count(distinct case when result = 'escalated' , timestamp between @time1 , @time2                            clientid end) numescalated, table t; 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -