I'm trying to find duplicate "keys" so that they can be addressed and made into proper, unique keys.
I recently learned that a HAVING clause can filter the results of an aggregate query by targeting the results of a GROUP BY. You GROUP BY the alleged "key" and HAVING where the count is > 1, and there are your problem rows.
My question is, what is the equivalent of this for windowing functions?
The following table should only be atomic to name and month, but it's using a date field that is detailed to the day (i.e. something can appear to happen twice or more times in a month when it should only be monthly).
select
event_id,
overly_specific_date,
count(*) over(partition by event_id, substring(convert(char(8), overly_specific_date), 0, 7))
from events_historic
order by over(partition by event_id, substring(convert(char(8), overly_specific_date), 0, 7))
vs
select
event_id,
count(*)
from events_historic
group by event_id, substring(convert(char(8), overly_specific_date), 0, 7)
having count(*) > 1
The first query is good because it shows what I want, but I'd like to filter it down. I know I could do it in a larger query or a CTE, but I'm looking for something concise like HAVING. The second query uses HAVING, but it no longer displays one part of the key, overly_specific_date.
How can I filter the second query?