SantasLittleHelper
Board Regular
- Joined
- Nov 25, 2016
- Messages
- 77
I have the 2 tables below:
Table1
Table2
I want to create Table 3 below based on this criteria.
In text, I want to add a True/False flag column to show if a Client ID had an open record between the two tables at the same time. The difficulty is that there can be multiple duplicate IDs in each table
Is anybody able to help with the SQL?
The Final table should look like this:
Table3
Table1
ID | Start Date | End Date |
---|---|---|
ABC123 | 01/04/2020 | 13/09/2020 |
DEF456 | 03/01/2020 | 17/02/2020 |
ABC123 | 15/09/2020 | 30/09/2020 |
Table2
ID | Start Date | End Date |
---|---|---|
ABC123 | 17/12/2019 | 12/07/2020 |
ABC123 | 13/07/2020 | 11/11/2020 |
DEF456 | 07/07/2020 | 09/09/2020 |
DEF456 | 24/09/2020 | 31/10/2020 |
I want to create Table 3 below based on this criteria.
In text, I want to add a True/False flag column to show if a Client ID had an open record between the two tables at the same time. The difficulty is that there can be multiple duplicate IDs in each table
Is anybody able to help with the SQL?
SQL:
IF
THERE IS A ROW
WHERE(
AND(
(Table1.ID = Table2.ID),
(Table1.[End date] >= Table2.[Start Date]),
(Table1.[Start date] <= Table2.[End Date]))
1,0)
The Final table should look like this:
Table3
ID | Start Date | End Date | Supported During |
---|---|---|---|
ABC123 | 17/12/2019 | 12/07/2020 | 1 |
ABC123 | 13/07/2020 | 11/11/2020 | 1 |
DEF456 | 07/07/2020 | 09/09/2020 | 0 |
DEF456 | 24/09/2020 | 31/10/2020 | 0 |