I have data that is structured like this:
ID Location Flag1 Flag2 Flag3 Date
A A1 0 1 0 01/01/2013
A A2 1 0 0 01/02/2013
A A2 1 0 0 01/02/2013
A A3 1 0 0 01/03/2013
A A3 0 1 0 01/03/2013
I need to count (among multiple IDs), the number of distinct Dates by distinct Locations with distinct Flags. The ideal outcome would look like this:
ID Date Count
A 01/01/2013 1
A 01/02/2013 1
A 01/03/2013 2
There are elements in the data that caused A with Location A2 to be distinct despite having the same Flag1 values but I don't care about those and want to roll up the data in the ideal outcome.
I've tried using this:
select count distinct ID, Location, Flag1, Flag2, Flag3, count (distinct
ID||Location||Date) as count
group by ID, Location
It doesn't seem to be right.
Here's one way to do it. Simply get the distinct records in a subquery and then count and group the data.
SELECT Id, [Date], COUNT(*) AS cnt
FROM
(
SELECT DISTINCT ID, Location, Flag1, Flag2, Flag3, [Date]
FROM YourTable
) AS data
GROUP BY data.Id, data.[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.