I have two tables in SQL Server database.
1 Rental
This table stores rental contract with its duration
CREATE TABLE [dbo].Rental ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[Rental] ON
GO
INSERT [dbo].[Rental] ([RentalId], [RentalContract], [StartDate], [EndDate]) VALUES (1, N'C1', CAST(N'2022-01-01' AS Date), CAST(N'2022-01-31' AS Date))
GO
INSERT [dbo].[Rental] ([RentalId], [RentalContract], [StartDate], [EndDate]) VALUES (2, N'C2', CAST(N'2022-02-01' AS Date), CAST(N'2022-02-15' AS Date))
GO
INSERT [dbo].[Rental] ([RentalId], [RentalContract], [StartDate], [EndDate]) VALUES (3, N'C3', CAST(N'2022-03-01' AS Date), CAST(N'2022-03-31' AS Date))
GO
INSERT [dbo].[Rental] ([RentalId], [RentalContract], [StartDate], [EndDate]) VALUES (4, N'C4', CAST(N'2022-04-01' AS Date), CAST(N'2022-04-30' AS Date))
GO
SET IDENTITY_INSERT [dbo].[Rental] OFF
GO
2 RentalInvoice
This table stores invoices created for the specified rental contract. The invoice also will be for specific duration.
CREATE TABLE [dbo].RentalInvoice ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[RentalInvoice] ON
GO
INSERT [dbo].[RentalInvoice] ([InvoiceId], [InvoiceNo], [InvoiceDate], [RentalId], [StartDate], [EndDate]) VALUES (1, N'A1', CAST(N'2022-01-15' AS Date), 1, CAST(N'2022-01-10' AS Date), CAST(N'2022-01-31' AS Date))
GO
INSERT [dbo].[RentalInvoice] ([InvoiceId], [InvoiceNo], [InvoiceDate], [RentalId], [StartDate], [EndDate]) VALUES (2, N'A2', CAST(N'2022-02-06' AS Date), 2, CAST(N'2022-02-02' AS Date), CAST(N'2022-02-06' AS Date))
GO
INSERT [dbo].[RentalInvoice] ([InvoiceId], [InvoiceNo], [InvoiceDate], [RentalId], [StartDate], [EndDate]) VALUES (3, N'A3', CAST(N'2022-03-11' AS Date), 3, CAST(N'2022-03-01' AS Date), CAST(N'2022-03-10' AS Date))
GO
INSERT [dbo].[RentalInvoice] ([InvoiceId], [InvoiceNo], [InvoiceDate], [RentalId], [StartDate], [EndDate]) VALUES (4, N'A4', CAST(N'2022-04-15' AS Date), 4, CAST(N'2022-04-01' AS Date), CAST(N'2022-04-15' AS Date))
GO
INSERT [dbo].[RentalInvoice] ([InvoiceId], [InvoiceNo], [InvoiceDate], [RentalId], [StartDate], [EndDate]) VALUES (6, N'A4', CAST(N'2022-04-25' AS Date), 4, CAST(N'2022-04-20' AS Date), CAST(N'2022-04-25' AS Date))
GO
SET IDENTITY_INSERT [dbo].[RentalInvoice] OFF
GO
I want a pending invoice list for each rental contract along with first pending date range for which invoice is to be created.
Something like
RentalContract, Pending Start Date, Pending End Date
Is it possible to get the output in single query?
I have tried as following:
SELECT r.*,
CASE
WHEN inv.MinInvStartDate IS NULL THEN r.StartDate
WHEN r.StartDate < inv.MinInvStartDate THEN r.StartDate
WHEN r.StartDate = inv.MinInvStartDate AND inv.MaxInvEndDate < r.EndDate THEN DATEADD(day, 1, inv.MaxInvEndDate)
ELSE NULL
END AS PendingStartDate,
CASE
WHEN inv.MaxInvEndDate IS NULL THEN r.EndDate
WHEN r.StartDate < inv.MinInvStartDate THEN DATEADD(day, -1, inv.MinInvStartDate)
WHEN r.StartDate = inv.MinInvStartDate AND inv.MaxInvEndDate < r.EndDate THEN r.EndDate
ELSE NULL
END AS PendingEndDate
from Rental r
LEFT JOIN (
SELECT RentalId, MIN(StartDate) AS MinInvStartDate, MAX(EndDate) AS MaxInvEndDate
from RentalInvoice
GROUP BY RentalId) inv
ON r.RentalId = inv.RentalId
The above sql returns proper PendingStartDate, PendingEndDate for RentalId = 1, 2 & 3. But for RentalId = 4, it is giving '2022-04-26' as PendingStartDate and '2022-04-30' as PendingEndDate... Actually I need the first pending duration, i.e. '2022-04-16' as PendingStartDate and '2022-04-19' as PendingEndDate.