Michael Cole
2015-11-12 00:25:23 UTC
I have the following data: -
CREATE TABLE [dbo].[Test](
[DateFrom] [date] NOT NULL,
[DateTo] [date] NOT NULL,
[ExpectedLine] [int] NULL
) ON [PRIMARY]
GO
INSERT [dbo].[Test] ([DateFrom], [DateTo], [ExpectedLine]) VALUES
(CAST(N'2015-01-01' AS Date), CAST(N'2015-01-10' AS Date), 1)
GO
INSERT [dbo].[Test] ([DateFrom], [DateTo], [ExpectedLine]) VALUES
(CAST(N'2015-01-12' AS Date), CAST(N'2015-01-16' AS Date), 1)
GO
INSERT [dbo].[Test] ([DateFrom], [DateTo], [ExpectedLine]) VALUES
(CAST(N'2015-01-15' AS Date), CAST(N'2015-01-20' AS Date), 2)
GO
INSERT [dbo].[Test] ([DateFrom], [DateTo], [ExpectedLine]) VALUES
(CAST(N'2015-01-18' AS Date), CAST(N'2015-01-24' AS Date), 1)
GO
INSERT [dbo].[Test] ([DateFrom], [DateTo], [ExpectedLine]) VALUES
(CAST(N'2015-01-25' AS Date), CAST(N'2015-01-30' AS Date), 1)
GO
INSERT [dbo].[Test] ([DateFrom], [DateTo], [ExpectedLine]) VALUES
(CAST(N'2015-02-01' AS Date), CAST(N'2015-02-07' AS Date), 1)
GO
INSERT [dbo].[Test] ([DateFrom], [DateTo], [ExpectedLine]) VALUES
(CAST(N'2015-02-03' AS Date), CAST(N'2015-02-08' AS Date), 2)
GO
INSERT [dbo].[Test] ([DateFrom], [DateTo], [ExpectedLine]) VALUES
(CAST(N'2015-02-06' AS Date), CAST(N'2015-02-12' AS Date), 3)
GO
INSERT [dbo].[Test] ([DateFrom], [DateTo], [ExpectedLine]) VALUES
(CAST(N'2015-02-11' AS Date), CAST(N'2015-02-20' AS Date), 1)
GO
INSERT [dbo].[Test] ([DateFrom], [DateTo], [ExpectedLine]) VALUES
(CAST(N'2015-02-16' AS Date), CAST(N'2015-02-24' AS Date), 2)
GO
Note that I also have a Date Dimension table (and an Integer Dimension
table if it becomes needed)
I am trying to write a query to return the Line Number, as per the
expected results listed in the table above. This line number is
calculated as follows: -
For each date range, in sequence of the From Date, assign the minimum
line number such that it does not have the same line number as any
prior date range for a date within the respective ranges.
If that doesn't make sense, I'll try to illustrate using the above
data.
Note - all dates in dd/m format.
Range 1 - 1/1 to 10/1 - gets Line 1
Range 2 - 12/1 to 16/1 - also gets Line 1
Range 3 - 15/1 to 20/1 - gets Line 2, as Line 1 for the 15th and 16th
is taken
Range 4 - 18/1 to 24/1 - gets Line 1 - whilst 18-20 are also taken by
Range 3, it was assigned to Line 2, so Line 1 is available
Range 5 - 25/1 to 30/1 - gets Line 1 - no issues
Range 6 - 1/2 to 7/2 - gets Line 1 - no issues
Range 7 - 3/2 to 8/2 - gets Line 2 - 3rd to 7th (Line 1) are taken by
above
Range 8 - 6/2 to 12/2 - gets Line 3 - for 6th and 7th, both Lines 1 and
2 are already taken
Range 9 - 11/2 to 20/2 - gets Line 1 - even though 11th and 12th are
taken by range 8, it was assigned Line 3
Range 10 - 16/2 to 24/2 - gets Line 2
Does this make sense? If not, I shall try to explain it better.
I would prefer a set-based solution, as there will be a lot of data to
go though (about 1500 ranges) and it needs a response time of less than
15 seconds maximum (less than 10 would be better.)
I have tried quite a few things and just cannot get the proper approach
- any advice would be helpful.
CREATE TABLE [dbo].[Test](
[DateFrom] [date] NOT NULL,
[DateTo] [date] NOT NULL,
[ExpectedLine] [int] NULL
) ON [PRIMARY]
GO
INSERT [dbo].[Test] ([DateFrom], [DateTo], [ExpectedLine]) VALUES
(CAST(N'2015-01-01' AS Date), CAST(N'2015-01-10' AS Date), 1)
GO
INSERT [dbo].[Test] ([DateFrom], [DateTo], [ExpectedLine]) VALUES
(CAST(N'2015-01-12' AS Date), CAST(N'2015-01-16' AS Date), 1)
GO
INSERT [dbo].[Test] ([DateFrom], [DateTo], [ExpectedLine]) VALUES
(CAST(N'2015-01-15' AS Date), CAST(N'2015-01-20' AS Date), 2)
GO
INSERT [dbo].[Test] ([DateFrom], [DateTo], [ExpectedLine]) VALUES
(CAST(N'2015-01-18' AS Date), CAST(N'2015-01-24' AS Date), 1)
GO
INSERT [dbo].[Test] ([DateFrom], [DateTo], [ExpectedLine]) VALUES
(CAST(N'2015-01-25' AS Date), CAST(N'2015-01-30' AS Date), 1)
GO
INSERT [dbo].[Test] ([DateFrom], [DateTo], [ExpectedLine]) VALUES
(CAST(N'2015-02-01' AS Date), CAST(N'2015-02-07' AS Date), 1)
GO
INSERT [dbo].[Test] ([DateFrom], [DateTo], [ExpectedLine]) VALUES
(CAST(N'2015-02-03' AS Date), CAST(N'2015-02-08' AS Date), 2)
GO
INSERT [dbo].[Test] ([DateFrom], [DateTo], [ExpectedLine]) VALUES
(CAST(N'2015-02-06' AS Date), CAST(N'2015-02-12' AS Date), 3)
GO
INSERT [dbo].[Test] ([DateFrom], [DateTo], [ExpectedLine]) VALUES
(CAST(N'2015-02-11' AS Date), CAST(N'2015-02-20' AS Date), 1)
GO
INSERT [dbo].[Test] ([DateFrom], [DateTo], [ExpectedLine]) VALUES
(CAST(N'2015-02-16' AS Date), CAST(N'2015-02-24' AS Date), 2)
GO
Note that I also have a Date Dimension table (and an Integer Dimension
table if it becomes needed)
I am trying to write a query to return the Line Number, as per the
expected results listed in the table above. This line number is
calculated as follows: -
For each date range, in sequence of the From Date, assign the minimum
line number such that it does not have the same line number as any
prior date range for a date within the respective ranges.
If that doesn't make sense, I'll try to illustrate using the above
data.
Note - all dates in dd/m format.
Range 1 - 1/1 to 10/1 - gets Line 1
Range 2 - 12/1 to 16/1 - also gets Line 1
Range 3 - 15/1 to 20/1 - gets Line 2, as Line 1 for the 15th and 16th
is taken
Range 4 - 18/1 to 24/1 - gets Line 1 - whilst 18-20 are also taken by
Range 3, it was assigned to Line 2, so Line 1 is available
Range 5 - 25/1 to 30/1 - gets Line 1 - no issues
Range 6 - 1/2 to 7/2 - gets Line 1 - no issues
Range 7 - 3/2 to 8/2 - gets Line 2 - 3rd to 7th (Line 1) are taken by
above
Range 8 - 6/2 to 12/2 - gets Line 3 - for 6th and 7th, both Lines 1 and
2 are already taken
Range 9 - 11/2 to 20/2 - gets Line 1 - even though 11th and 12th are
taken by range 8, it was assigned Line 3
Range 10 - 16/2 to 24/2 - gets Line 2
Does this make sense? If not, I shall try to explain it better.
I would prefer a set-based solution, as there will be a lot of data to
go though (about 1500 ranges) and it needs a response time of less than
15 seconds maximum (less than 10 would be better.)
I have tried quite a few things and just cannot get the proper approach
- any advice would be helpful.
--
Michael Cole
Michael Cole