Created
May 27, 2022 18:56
-
-
Save jingyang-li/2efff23413bec8bc201492a742e0a875 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE [dbo].[RentalInvoice]( | |
[InvoiceId] [int] IDENTITY(1,1) NOT NULL, | |
[InvoiceNo] [varchar](15) NOT NULL, | |
[InvoiceDate] [date] NOT NULL, | |
[RentalId] [int] NOT NULL, | |
[StartDate] [date] NOT NULL, | |
[EndDate] [date] NOT NULL, | |
CONSTRAINT [PK_RentalInvoice] PRIMARY KEY CLUSTERED | |
( | |
[InvoiceId] ASC | |
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] | |
) 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 | |
CREATE TABLE [dbo].[Rental]( | |
[RentalId] [int] IDENTITY(1,1) NOT NULL, | |
[RentalContract] [varchar](50) NOT NULL, | |
[StartDate] [date] NOT NULL, | |
[EndDate] [date] NOT NULL, | |
CONSTRAINT [PK_Rental] PRIMARY KEY CLUSTERED | |
( | |
[RentalId] ASC | |
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] | |
) 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 | |
;WITH Num1 (n) AS ( | |
SELECT 1 as n | |
UNION ALL SELECT n+1 as n | |
FROM Num1 Where n <101), | |
Num2 (n) AS (SELECT 1 FROM Num1 AS X, Num1 AS Y), | |
Nums (n) AS (SELECT ROW_NUMBER() OVER(ORDER BY n) FROM Num2) | |
,mycteRental as ( | |
Select RentalId,[RentalContract], dateadd(day,n-1,StartDate) dt | |
from [Rental] | |
Cross apply (select n from nums ) d(n) | |
where dateadd(day,n-1,StartDate) <= [EndDate] | |
) | |
,mycteRentalInvoice as ( | |
Select RentalId, dateadd(day,n-1,StartDate) dt | |
from [RentalInvoice] | |
Cross apply (select n from nums ) d(n) | |
where dateadd(day,n-1,StartDate) <= [EndDate] | |
) | |
,mycteFindIsland as ( | |
select *,DATEADD(DAY, - ROW_NUMBER() OVER(PARTITION BY RentalId ORDER BY dt), dt) AS grp | |
from mycteRental m1 | |
where not exists | |
(select 1 from mycteRentalInvoice m2 where m1.RentalId=m2.RentalId and m1.dt=m2.dt) | |
) | |
,mycteFindFirstIsland as ( | |
select *,dense_rank() OVER(PARTITION BY RentalId Order by grp) dnk | |
from mycteFindIsland | |
) | |
select RentalId,RentalContract, min(dt) StartDate, max(dt) EndDate | |
from mycteFindFirstIsland | |
where dnk=1 -- get first | |
group by RentalId,RentalContract | |
drop table [dbo].[RentalInvoice], [dbo].[Rental] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment