I have a table with a couple thousend unique IDs. Every ID has around 200-300 EventCodes. Here is an example table:
| ID | EventCode |
|---|---|
| 1 | A01G0 |
| 1 | B428 |
| 2 | N0030 |
| 2 | B428 |
| 3 | CD45 |
| 3 | B428 |
I would like to get the distinct ID when specific EventCodes are not present. For example, when "A01G0" or "N0030" are present, skip the ID 1 and 2.
Desired Output should then be:
| ID |
|---|
| 3 |
I found this solution but it is not suitable for daily use because of lack of speed:
WITH cte
AS (SELECT
ev.id as id
,ev.EventCode AS EventCode
,ev.EventDate
,ROW_NUMBER() OVER ( PARTITION BY ev.id ORDER BY ev.EventDate DESC) AS rn
FROM eventTable ev
WHERE NOT EXISTS( SELECT *
FROM eventTable
WHERE ev.id = ev.id
AND ev.id NOT IN ('A01G0','A01G2','N0030','N0032','UN030')
))
SELECT id
FROM cte
WHERE rn = 1
Any ideas a a simpler approach would be really appreciated!