简体   繁体   中英

Select count distinct query

Writing a query that will give me the total count of an event rather than counting the event every time it happens

Ok so I'm trying to write a query that will count the number of session IDs for that each distinct account id, program id and device id has.So for example. acct id 1 has 3 devices watching the same program with 3 different session IDs. I want my query to give me Acct id 1 has 3 sessions. The current query i have is just listing the number of sessions like this

Acct ID 01 Session 1, Acct ID 01 Session 1


SELECT count( DISTINCT source_session_id), acct_id, device_id, program_id,  event_date
FROM TABLE 
WHERE condition 
group by source_session_id, acct_id, device_id, program_id, event_date;

I would like the end result of my query to be Acct ID 01 # of Sessions 2

Try removing source_session_id from the Group By clause or you will get a row for each different source_session_id.

SELECT count( DISTINCT source_session_id), acct_id, device_id, program_id,  event_date
FROM TABLE 
WHERE condition 
group by acct_id, device_id, program_id, event_date;

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