I have a table look like this:
ID B C date01 date02 date03
11 xxx xxxx 2020-05-01 2020-05-02 2020-06-02
11 xxx xxxx 2020-06-01 2020-06-03 2020-05-02
11 xxx xxxx 2020-07-01 2020-07-03 2020-06-30
11 xxx xxxx 2020-07-01 2020-06-03 2020-06-30
11 xxx xxxx 2020-01-01 2020-01-08 2020-05-02
11 xxx xxxx 2020-02-01 2020-01-31 2020-05-02
22 xxx xxxx 2020-05-01 2020-05-02 2020-06-02
22 xxx xxxx 2020-06-01 2020-06-03 2020-05-02
22 xxx xxxx 2020-07-01 2020-07-03 2020-06-30
22 xxx xxxx 2020-07-01 2020-06-03 2020-06-30
22 xxx xxxx 2020-01-01 2020-01-08 2020-05-02
22 xxx xxxx 2020-02-01 2020-01-31 2020-05-02
I want to return everything but with a latest date of those three dates for each ID, and date02 cannot be later than date03, my current output will give me this where date02 > date03:
11 xxx xxxx 2020-07-01 2020-07-03 2020-06-30
Expected output:
11 xxx xxxx 2020-07-01 2020-06-03 2020-06-30
22 xxx xxxx 2020-07-01 2020-06-03 2020-06-30
I tried this:
SELECT
id,
B,
C,
max(date01),
max(date02),
max(date03),
FROM
table
WHERE
'date02' < 'date03'
GROUP BY id
I've added WHERE 'date02' < 'date03'
but why the output still have the records where date02>date03?? I'm very new to SQL, please help...