How to dynamically compare x number of tables and generate dynamic messages?
I have the following schema:
- Table1: customerID, startDate, endDate
- Table2: customerID, startDate, endDate
- Table3: customerID, startDate, endDate
And so on.
Customers in Table 1 could exist in Table 2 or 3 (or not at all). And same with customers in table 2 or 3, they may have common customers or not at all.
Expected Result:
Basically a table with result (whether Customer is in Table 1/2/3/x), and result detail (Table 1 StartDate< Table 2 StartDate and so on). Each row contains a customer.
What I did:
- I created a giant case statement. It works, but it has many issues: it takes long to write, it is not dynamic, as you add more tables, you will end up with a lot more cases, and it never ends. The issue is also that it could add error and becomes impossible for more than 3 or 4 tables.
- I tried to separate them into columns then concat them, but still couldn't manage it.
My table structure:
CREATE TABLE [dbo].[Table_1](
[CustomerID] [varchar](15) NULL,
[StartDate] [date] NULL,
[EndDate] [date] NULL)
/****** Object: Table [dbo].[Table_2] Script Date: 2021-08-18 11:50:24 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Table_2](
[CustomerID] [varchar](15) NULL,
[StartDate] [date] NULL,
[EndDate] [date] NULL
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[Table_3] Script Date: 2021-08-18 11:50:24 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Table_3](
[CustomerID] [varchar](15) NULL,
[StartDate] [date] NULL,
[EndDate] [date] NULL
) ON [PRIMARY]
GO
INSERT [dbo].[Table_1] ([CustomerID], [StartDate], [EndDate]) VALUES (N'0001', CAST(N'2020-01-20' AS Date), CAST(N'2020-01-25' AS Date))
GO
INSERT [dbo].[Table_1] ([CustomerID], [StartDate], [EndDate]) VALUES (N'0002', CAST(N'2020-01-21' AS Date), CAST(N'2020-01-26' AS Date))
GO
INSERT [dbo].[Table_1] ([CustomerID], [StartDate], [EndDate]) VALUES (N'0003', CAST(N'2020-01-22' AS Date), CAST(N'2020-01-27' AS Date))
GO
INSERT [dbo].[Table_1] ([CustomerID], [StartDate], [EndDate]) VALUES (N'0004', CAST(N'2020-01-23' AS Date), CAST(N'2020-01-28' AS Date))
GO
INSERT [dbo].[Table_1] ([CustomerID], [StartDate], [EndDate]) VALUES (N'0005', CAST(N'2020-01-24' AS Date), CAST(N'2020-01-29' AS Date))
GO
INSERT [dbo].[Table_1] ([CustomerID], [StartDate], [EndDate]) VALUES (N'0006', CAST(N'2020-03-15' AS Date), CAST(N'2020-03-28' AS Date))
GO
INSERT [dbo].[Table_2] ([CustomerID], [StartDate], [EndDate]) VALUES (N'0001', CAST(N'2020-01-20' AS Date), CAST(N'2020-01-25' AS Date))
GO
INSERT [dbo].[Table_2] ([CustomerID], [StartDate], [EndDate]) VALUES (N'0002', CAST(N'2020-01-20' AS Date), CAST(N'2020-01-26' AS Date))
GO
INSERT [dbo].[Table_2] ([CustomerID], [StartDate], [EndDate]) VALUES (N'0003', CAST(N'2020-01-22' AS Date), CAST(N'2020-01-25' AS Date))
GO
INSERT [dbo].[Table_2] ([CustomerID], [StartDate], [EndDate]) VALUES (N'0004', CAST(N'2020-01-20' AS Date), CAST(N'2020-01-21' AS Date))
GO
INSERT [dbo].[Table_2] ([CustomerID], [StartDate], [EndDate]) VALUES (N'0006', CAST(N'2020-01-15' AS Date), CAST(N'2020-01-28' AS Date))
GO
INSERT [dbo].[Table_3] ([CustomerID], [StartDate], [EndDate]) VALUES (N'0001', CAST(N'2020-01-20' AS Date), CAST(N'2020-01-25' AS Date))
GO
INSERT [dbo].[Table_3] ([CustomerID], [StartDate], [EndDate]) VALUES (N'0003', CAST(N'2020-01-22' AS Date), CAST(N'2020-01-25' AS Date))
GO
INSERT [dbo].[Table_3] ([CustomerID], [StartDate], [EndDate]) VALUES (N'0004', CAST(N'2020-01-23' AS Date), CAST(N'2020-01-28' AS Date))
GO
INSERT [dbo].[Table_3] ([CustomerID], [StartDate], [EndDate]) VALUES (N'0006', CAST(N'2020-01-19' AS Date), CAST(N'2020-01-28' AS Date))
GO