SQL or R Solution needed (prefer SQL).
I have 3 datasets, all with a common identifier. After appending Table 2 and Table 3 to Table 1 using FROM
Table 1
Common ID | District | Floor |
---|---|---|
A001 | East | High |
A002 | South | Low |
A003 | West | Med |
A004 | North | High |
A005 | East | High |
A006 | South | Low |
A007 | West | Med |
A008 | North | High |
A009 | West | High |
Table 2
Common ID | Last Enquiry Date |
---|---|
A001 | 31/01/2022 |
A001 | 18/07/2022 |
A002 | 20/05/2021 |
A002 | 20/05/2020 |
A002 | 20/05/2022 |
A003 | |
A004 | 13/05/2022 |
A005 | 18/08/2021 |
A005 | 18/08/2020 |
A006 | |
A007 | |
A008 | 27/01/2021 |
A009 | 14/02/2020 |
Table 3
Common ID | Last Completion Date |
---|---|
A001 | |
A002 | 29/11/2021 |
A002 | 29/11/2020 |
A002 | 20/05/2022 |
A003 | |
A004 | 13/05/2022 |
A005 | 22/08/2020 |
A006 | 21/07/2019 |
A007 | 24/04/2018 |
A007 | 24/04/2019 |
A007 | 24/04/2021 |
A008 | |
A009 | 15/05/2021 |
A009 | 15/12/2021 |
In SQL, tried the solution here but missed the empty rows. If I replace Inner Join with Full Outer Join, the max.date effect will be gone.
how do I query sql for a latest record date for each user
Ideal output: | Common ID | District | Floor | Last Enquiry Date | Last Completion Date| | -------- | -------- | ------| --------------- | --------------- | | A001 | East | High | 18/07/2022 | | | A002 | South | Low | 20/05/2022 | 29/11/2021 | | A003 | West | Med | | | | A004 | North | High | 13/05/2022 | 13/05/2022 | | A005 | East | High | 18/08/2021 | 22/08/2020 | | A006 | South | Low | | 21/07/2019 | | A007 | West | Med | | 24/04/2021 | | A008 | North | High | 27/01/2021 | | | A009 | West | High | 14/02/2020 | 15/12/2021 |