I am trying to figure this situation out but for the life of me I don't know how to go about it.
Data in Table1 -
Activity_Date Code ID APDC_Date
29/04/21 08:03:38 AM 2 1 29/04/21
30/04/21 03:55:37 PM 12 1 29/04/21
29/04/21 08:03:38 AM 12 2 29/04/21
30/04/21 03:55:37 PM 2 2 29/04/21
29/04/21 08:03:38 AM 11 3 29/04/21
30/04/21 03:55:37 PM 12 3 29/04/21
Condition If the Max Activity Date Record for that id has a code <> 2 and has another line item for that ID where a code is 2 , display all records for that ID except the line item that has a code is 2 If the Max Activity Date Record has a code = 2 then nothing is displayed Otherwise display all records
What I want to be returned if I use the above table data, select code,id
Code ID
12 1
11 3
12 3
My current sql statement is pretty simple, I have the current statement in an AS (I have other tables I am accessing/joining)
Sara As (
SELECT Code, ID
FROM table1 a
WHERE a.apdc_date = (SELECT MAX(aa.apdc_date)
FROM table1 aa
WHERE aa.id = a.id
))
and then combining those results using a join
Left JOIN sara j
ON b.table2_id = j.id
I know there is an easy way or doing this but at this point in time I can't think of a way.
Any help would be appreciated.