Table 2
ID DATE
-- ----------
01 2018-04-01
03 2017-06-23
I am having hard time trying to write a SQL that will return the ID that has no date or minimum date based on the IDs
for example, for ID 01, 03 only, the sql should return 03 because between 01 and 03, 03 has the earlier date.
but for ID 01,02,03 only, the sql should return 02 because 02 has no data.
for all IDs, 01,02,03,04 the sql can return either 02 or 04.
I've been playing around with MIN(DATE) and NOT EXISTS clauses but no luck because I do not know how to return the empty data.
Any sample SQL is greatly appreciated!
SQL I tried
SELECT MIN(CASE WHEN DATE IS NULL THEN '1900-01-01' ELSE DATE END)
FROM TABLE2
WHERE ID IN (01,02,03,04)
but above sql will give me the min of existing dates. won't default non existing date to 1900-01-01.