I need help in creating a query that gives me a correct output.
I have a table with the sample output:
ID | Hour |
---|---|
1 | 09:00 |
1 | 11:00 |
2 | 09:00 |
2 | 11:00 |
3 | 09:00 |
3 | 10:00 |
3 | 11:00 |
4 | 09:00 |
4 | 11:00 |
5 | 11:00 |
The output should look like:
CNT_ID | Hour |
---|---|
4 | 09:00 |
1 | 11:00 |
All other entries should be ignored.
How can I create this query I already tried it with MIN(HOUR) but this doesn't work, because if an ID with only 11:00 exists it won't be shown.
You want to count each ID only in the first hour it occurs. That is easily done: Get the minimum hour per ID, then count per hour:
select hour, count(*)
from
(
select id, min(hour) as hour
from mytable
group by id
) each_id_with_its_first_hour
group by hour
order by hour;