here is the example table with data (rn column is ROW_NUMBER() for each UELN).
UELN OwnerID Date rn
191001180010389 017581 1989-06-30 00:00:00.000 1
191001180010389 017747 2011-06-02 00:00:00.000 2
191001180010389 017992 2014-03-25 00:00:00.000 3
191001180010389 117030 2015-02-03 00:00:00.000 4
191001250009303 018148 2004-06-30 00:00:00.000 1
191001250009303 018418 2013-10-16 00:00:00.000 2
I need to combine those rows to get result set like this:
UELN OwnerID DateFrom DateTo
191001180010389 017581 1989-06-30 00:00:00.000 2011-06-02 00:00:00.000
191001180010389 017747 2011-06-02 00:00:00.000 2014-03-25 00:00:00.000
191001180010389 017992 2014-03-25 00:00:00.000 2015-02-03 00:00:00.000
191001180010389 117030 2015-02-03 00:00:00.000 NULL
191001250009303 018148 2004-06-30 00:00:00.000 2013-10-16 00:00:00.000
191001250009303 018418 2013-10-16 00:00:00.000 NULL
NULL in DateTo column means that this is still valid.
Can anyone help me with the query?
select u1.*, u2.date as [date to]
from tabl u1
left join tabl u2
on u1.UELN = u2.UELN
and u2.rn = u1.rn + 1
You just need a left self join
The left part is what gets the null date for the no match
Using OUTER APPLY:
SELECT
t.UELN,
t.OwnerID,
DateFrom = t.[Date],
DateTo = x.DateTo
FROM tbl t
OUTER APPLY(
SELECT
DateTo = [Date]
FROM tbl
WHERE
UELN = t.UELN
AND rn = t.rn + 1
)x
You can try the following:
-- Create demo data
CREATE TABLE #temp(ueln bigint, ownerid nvarchar(20), date date, rn int)
INSERT INTO #temp(ueln, ownerid, date, rn)
VALUES (191001180010389,N'017581', N'1989-06-30 00:00:00.000', 1),
(191001180010389,N'017747', N'2011-06-02 00:00:00.000', 2),
(191001180010389,N'017992', N'2014-03-25 00:00:00.000', 3),
(191001180010389,N'117030', N'2015-02-03 00:00:00.000', 4),
(191001250009303,N'018148', N'2004-06-30 00:00:00.000', 1),
(191001250009303,N'018148', N'2013-10-16 00:00:00.000', 2)
-- your part
SELECT cur.ueln, cur.ownerid, cur.date as date_from, due.date as date_to
-- Maybe 1 day befor: DATEADD(day,-1,due.date) as date_to
FROM #temp as cur
LEFT JOIN #temp as due
ON cur.ueln = due.ueln
AND cur.rn = due.rn+1
DROP TABLE #temp
Which results in:
ueln ownerid date_from date_to
-------------------- -------------------- ---------- ----------
191001180010389 017581 1989-06-30 NULL
191001180010389 017747 2011-06-02 1989-06-30
191001180010389 017992 2014-03-25 2011-06-02
191001180010389 117030 2015-02-03 2014-03-25
191001250009303 018148 2004-06-30 NULL
191001250009303 018148 2013-10-16 2004-06-30
If you want to be the date_to one day before the next data-row, you can use the commented date_to.
We can use the LEAD() function introduced in SQL 2012.
--setup
CREATE TABLE #temp(ueln bigint, ownerid nvarchar(20), [date] date)
INSERT INTO #temp(ueln, ownerid, [date])
VALUES (191001180010389, N'017581', N'1989-06-30 00:00:00.000'),
(191001180010389, N'017747', N'2011-06-02 00:00:00.000'),
(191001180010389, N'017992', N'2014-03-25 00:00:00.000'),
(191001180010389, N'117030', N'2015-02-03 00:00:00.000'),
(191001250009303, N'018148', N'2004-06-30 00:00:00.000'),
(191001250009303, N'018148', N'2013-10-16 00:00:00.000');
--actual query
SELECT [ueln] ,
[ownerid] ,
[date] AS [DateFrom]
, LEAD([date], 1) OVER (PARTITION BY ueln ORDER BY [date]) AS [DateTo]
FROM #temp
Thanks to #Ionic for the temp table definition!
Related
I have a table which represents the number of hours worked for a specific day. From that table I want to be able to add 7 more columns to each row that represent the hours worked on the previous 7 days.
I've been using LAG() to accomplish that and it was working fine until I found an issue where I had a gap between days and now instead of picking the previous 7 days I'm picking the 7 previous rows.
This script demonstrates what I'm trying to accomplish.
DECLARE #data TABLE (
[user_id] int,
[date] DATETIME,
[day_hours_worked] VARCHAR(15)
);
INSERT INTO #data
VALUES
( '1', '2019-09-07 00:00:00.000', '07_8' )
, ( '1', '2019-09-08 00:00:00.000', '08_4' )
, ( '1', '2019-09-09 00:00:00.000', '09_6' )
, ( '1', '2019-09-10 00:00:00.000', '10_8' )
, ( '1', '2019-09-11 00:00:00.000', '11_4' )
, ( '1', '2019-09-12 00:00:00.000', '12_6' )
, ( '1', '2019-09-13 00:00:00.000', '13_8' )
, ( '1', '2019-09-14 00:00:00.000', '14_4' )
, ( '1', '2019-09-20 00:00:00.000', '20_8' );
select
[user_id],
[date],
[day_hours_worked],
LAG([day_hours_worked], 1) OVER (PARTITION BY [user_id] ORDER BY [date]) [Hours-1],
LAG([day_hours_worked], 2) OVER (PARTITION BY [user_id] ORDER BY [date]) [Hours-2],
LAG([day_hours_worked], 3) OVER (PARTITION BY [user_id] ORDER BY [date]) [Hours-3],
LAG([day_hours_worked], 4) OVER (PARTITION BY [user_id] ORDER BY [date]) [Hours-4],
LAG([day_hours_worked], 5) OVER (PARTITION BY [user_id] ORDER BY [date]) [Hours-5],
LAG([day_hours_worked], 6) OVER (PARTITION BY [user_id] ORDER BY [date]) [Hours-6],
LAG([day_hours_worked], 7) OVER (PARTITION BY [user_id] ORDER BY [date]) [Hours-7]
from #data
This is what I get.
user_id date day_hours_worked Hours-1 Hours-2 Hours-3 Hours-4 Hours-5 Hours-6 Hours-7
1 2019-09-07 00:00:00.000 07_8 NULL NULL NULL NULL NULL NULL NULL
1 2019-09-08 00:00:00.000 08_4 07_8 NULL NULL NULL NULL NULL NULL
1 2019-09-09 00:00:00.000 09_6 08_4 07_8 NULL NULL NULL NULL NULL
1 2019-09-10 00:00:00.000 10_8 09_6 08_4 07_8 NULL NULL NULL NULL
1 2019-09-11 00:00:00.000 11_4 10_8 09_6 08_4 07_8 NULL NULL NULL
1 2019-09-12 00:00:00.000 12_6 11_4 10_8 09_6 08_4 07_8 NULL NULL
1 2019-09-13 00:00:00.000 13_8 12_6 11_4 10_8 09_6 08_4 07_8 NULL
1 2019-09-14 00:00:00.000 14_4 13_8 12_6 11_4 10_8 09_6 08_4 07_8
1 2019-09-20 00:00:00.000 20_8 14_4 13_8 12_6 11_4 10_8 09_6 08_4
The last row is the only incorrect one because it's looking at the previous 7 rows instead of the previous 7 days. In my scenario the expected result should be:
user_id date day_hours_worked Hours-1 Hours-2 Hours-3 Hours-4 Hours-5 Hours-6 Hours-7
1 2019-09-07 00:00:00.000 07_8 NULL NULL NULL NULL NULL NULL NULL
1 2019-09-08 00:00:00.000 08_4 07_8 NULL NULL NULL NULL NULL NULL
1 2019-09-09 00:00:00.000 09_6 08_4 07_8 NULL NULL NULL NULL NULL
1 2019-09-10 00:00:00.000 10_8 09_6 08_4 07_8 NULL NULL NULL NULL
1 2019-09-11 00:00:00.000 11_4 10_8 09_6 08_4 07_8 NULL NULL NULL
1 2019-09-12 00:00:00.000 12_6 11_4 10_8 09_6 08_4 07_8 NULL NULL
1 2019-09-13 00:00:00.000 13_8 12_6 11_4 10_8 09_6 08_4 07_8 NULL
1 2019-09-14 00:00:00.000 14_4 13_8 12_6 11_4 10_8 09_6 08_4 07_8
1 2019-09-20 00:00:00.000 20_8 NULL NULL NULL NULL NULL 14_4 13_8
Is this even possible using LAG()? Is there a better way to accomplish what I want?
Thanks in advance.
I don't think there is an elegant way to do this in SQL Server. The simplest may be a case expression:
(case when lag(date, 1) over (partition by user_id order by date) = dateadd(day, 1, date)
then lag(day_hours_worked, 1) over (partition by [user_id] order by [date])
end) as [Hours-1],
Some databases support range with time intervals, making this simpler.
EDIT:
You can do what you want using apply:
select d.*, d2.*, d2.*
from data d outer apply
(select max(case when dd.diff = 1 then d2.day_hours_worked end) as w1,
max(case when dd.diff = 2 then d2.day_hours_worked end) as w2,
max(case when dd.diff = 3 then d2.day_hours_worked end) as w3,
max(case when dd.diff = 4 then d2.day_hours_worked end) as w4,
max(case when dd.diff = 5 then d2.day_hours_worked end) as w5,
max(case when dd.diff = 6 then d2.day_hours_worked end) as w6,
max(case when dd.diff = 7 then d2.day_hours_worked end) as w7
from data d2 cross apply
(values (datediff(day, d2.date, d.date) )) dd(diff)
where d2.date < d.date and d2.date >= dateadd(day, -7, d.date)
) d2;
Here is a db<>fiddle.
Your logic looks correct and it only fails because of missing dates. So I thought if we add missing dates, your logic will work.
Please follow my code. You may need to change some parts of that but it gives you the result you want for one single user.
;with wrk as (
select
[user_id],
[date],
[day_hours_worked]
from #data
)
-- generate list of dates, starting from min date in your table
, allDates as (
select Dt = dateadd(day, row_number() over (order by object_id) - 1, (select min([date]) from wrk))
from sys.objects
)
-- select only dates missing from your table
, missingDates as (
select Dt
from allDates
where Dt < (select max([date]) from wrk)
and not exists (select [date] from wrk where wrk.[date] = Dt)
)
, fullData as (
select [user_id],
[date],
[day_hours_worked]
from wrk
union
-- we need [user_id] here, so I get it from your table
-- this is not perfect and works only if you work on one single user
select (select top 1 [user_id] from wrk), Dt, null
from missingDates
)
, final as (
select
[user_id],
[date],
[day_hours_worked],
LAG([day_hours_worked], 1) OVER (PARTITION BY [user_id] ORDER BY [date]) [Hours-1],
LAG([day_hours_worked], 2) OVER (PARTITION BY [user_id] ORDER BY [date]) [Hours-2],
LAG([day_hours_worked], 3) OVER (PARTITION BY [user_id] ORDER BY [date]) [Hours-3],
LAG([day_hours_worked], 4) OVER (PARTITION BY [user_id] ORDER BY [date]) [Hours-4],
LAG([day_hours_worked], 5) OVER (PARTITION BY [user_id] ORDER BY [date]) [Hours-5],
LAG([day_hours_worked], 6) OVER (PARTITION BY [user_id] ORDER BY [date]) [Hours-6],
LAG([day_hours_worked], 7) OVER (PARTITION BY [user_id] ORDER BY [date]) [Hours-7]
from fullData
)
select *
from final
where day_hours_worked is not null
order by [date]
I have a 2 tables that look like this:
MonthEndDate
2016-06-30 00:00:00.000
2016-07-31 00:00:00.000
2016-08-31 00:00:00.000
2016-09-30 00:00:00.000
2016-10-31 00:00:00.000
2016-11-30 00:00:00.000
2016-12-31 00:00:00.000
AND
MonthEndDate CustomerId Flag
2016-06-30 00:00:00.000 123 1
2016-07-31 00:00:00.000 123 1
2016-08-31 00:00:00.000 123 1
2016-09-30 00:00:00.000 123 1
I would like an output that looks like this:
MonthEndDate CustomerId Flag
2016-06-30 00:00:00.000 123 1
2016-07-31 00:00:00.000 123 1
2016-08-31 00:00:00.000 123 1
2016-09-30 00:00:00.000 123 1
2016-10-31 00:00:00.000 123 0
2016-11-30 00:00:00.000 123 0
2016-12-31 00:00:00.000 123 0
Table 1 is a DimDate table that has month end date.
Table
2 is the CustomerInfo table.
Each customer has a Flag set to 1 whenever that customer has a value for the given Month End.
I want to get an output that will have every Month End Date (that's why I'm suing DimDate table) and when a customer does not have a value for the Month End I want the flag to show 0.
I'm using SQL Server 2005
Here is some sample code I used:
DECLARE #table1 TABLE
(
MonthEndDate DATETIME
)
INSERT INTO #table1
VALUES('2016-06-30 00:00:00.000')
INSERT INTO #table1
VALUES('2016-07-31 00:00:00.000')
INSERT INTO #table1
VALUES('2016-08-31 00:00:00.000')
INSERT INTO #table1
VALUES('2016-09-30 00:00:00.000')
INSERT INTO #table1
VALUES('2016-10-31 00:00:00.000')
INSERT INTO #table1
VALUES('2016-11-30 00:00:00.000')
INSERT INTO #table1
VALUES('2016-12-31 00:00:00.000')
DECLARE #table2 TABLE
(
MonthEndDate DATETIME
,CustomerId INT
,Flag INT
)
INSERT INTO #table2
VALUES('2016-06-30 00:00:00.000',123,1)
INSERT INTO #table2
VALUES('2016-07-31 00:00:00.000',123,1)
INSERT INTO #table2
VALUES('2016-08-31 00:00:00.000',123,1)
INSERT INTO #table2
VALUES('2016-09-30 00:00:00.000',123,1)
SELECt * FROM #table1
SELECt * FROM #table2
You need to do a CROSS JOIN on to get all combinations of MonthEndDate and CustomerId. When you have that, do a LEFT JOIN on table2 to get the Flag:
SELECT
t1.MonthEndDate,
c.CustomerId,
Flag = ISNULL(t2.Flag, 0)
FROM #table1 t1
CROSS JOIN (SELECT DISTINCT CustomerId FROM #table2) c
LEFT JOIN #table2 t2
ON t1.MonthEndDate = t2.MonthEndDate
AND c.CustomerId = t2.CustomerId
I think you just want a left join:
select t1.*, coalesce(t2.flag, 0) as flag
from #table1 t1 left join
#table2 t2
on t1.MonthEndDate = t2.MonthEndDate;
how to club as 1 record if employee has worked continuously from feb 1 to feb 15th.
please help me
example
scenario 1.emp who has worked continuously.
empid datebegin dateend
1 2017-02-01 2017-02-05
1 2017-02-06 2017-02-08
1 2017-02-09 2017-02-15
desired O/P: 1 2017-02-01 2017-02-15
scenario2:not worked continuously
empid datebegin dateend
1 2017-02-01 2017-02-05
1 2017-02-07 2017-02-08
1 2017-02-09 2017-02-15
desired O/P:
empid datebegin dateend
1 2017-02-01 2017-02-05
1 2017-02-07 2017-02-15
Use MAX and MIN aggregate funtcions :
CREATE TABLE #Table(empid INT, datebegin DATE , dateend DATE)
INSERT INTO #Table( empid , datebegin , dateend)
SELECT 1,'2017-02-01','2017-02-05' UNION ALL
SELECT 1,'2017-02-06','2017-02-08' UNION ALL
SELECT 1,'2017-02-09','2017-02-15'
SELECT empid , MIN(datebegin) datebegin ,MAX(dateend) dateend
FROM #Table
GROUP BY empid
Using a Common table expression and ROW_NUmber this is possible like so
DECLARE #sample TABLE (empid INT, datebegin DATE, dateend DATE )
INSERT INTO #sample
( empid, datebegin, dateend )
VALUES
(1,'2017-02-01','2017-02-05' )
,(1,'2017-02-06','2017-02-08' )
,(1,'2017-02-09','2017-02-15' )
,(2,'2017-02-01','2017-02-05' )
,(2,'2017-02-07','2017-02-08' )
,(2,'2017-02-09','2017-02-15' )
;WITH cteX
AS(
SELECT
ROW_NUMBER()OVER (ORDER BY S.empid, S.datebegin) 'RN'
, S.empid
, S.datebegin
, S.dateend
FROM #sample S
)
SELECT
S.RN
, S.empid
, S.datebegin
, S.dateend
, DATEDIFF(DAY,S.dateend, S1.datebegin ) 'Diff'
, S1.datebegin
, S1.dateend
FROM cteX S
JOIN
cteX S1 ON S1.empid = S.empid AND S1.RN = S.RN + 1
ORDER BY S.RN
produces following output
empid datebegin dateend
1 2017-02-01 2017-02-15
2 2017-02-01 2017-02-05
2 2017-02-07 2017-02-15
Here is the table that I am working with:
MemberID MembershipStartDate MembershipEndDate
=================================================================
123 2010-01-01 00:00:00.000 2012-12-31 00:00:00.000
123 2011-01-01 00:00:00.000 2012-12-31 00:00:00.000
123 2013-05-01 00:00:00.000 2013-12-31 00:00:00.000
123 2014-01-01 00:00:00.000 2014-12-31 00:00:00.000
123 2015-01-01 00:00:00.000 2015-03-31 00:00:00.000
What I want is to create one row that shows continuous membership,
and a second row if the membership breaks by more than 2 days, with a new start and end date..
So the output I am looking for is like:
MemberID MembershipStartDate MembershipEndDate
=================================================================
123 2010-01-01 00:00:00.000 2012-12-31 00:00:00.000
123 2013-05-01 00:00:00.000 2015-03-31 00:00:00.000
There is a memberID field attached to these dates which is how they are grouped.
I've had to deal with this kind of thing before
I use something like this
USE tempdb
--Create test Data
DECLARE #Membership TABLE (MemberID int ,MembershipStartDate date,MembershipEndDate date)
INSERT #Membership
(MemberID,MembershipStartDate,MembershipEndDate)
VALUES (123,'2010-01-01','2012-12-31'),
(123,'2011-01-01','2012-12-31'),
(123,'2013-05-01','2013-12-31'),
(123,'2014-01-01','2014-12-31'),
(123,'2015-01-01','2015-03-31')
--Create a table to hold all the dates that might be turning points
DECLARE #SignificantDates Table(MemberID int, SignificantDate date, IsMember bit DEFAULT 0)
--Populate table with the start and end dates as well as the days just before and just after each period
INSERT #SignificantDates (MemberID ,SignificantDate)
SELECT MemberID, MembershipStartDate FROM #Membership
UNION
SELECT MemberID,DATEADD(day,-1,MembershipStartDate ) FROM #Membership
UNION
SELECT MemberID,MembershipEndDate FROM #Membership
UNION
SELECT MemberID,DATEADD(day,1,MembershipEndDate) FROM #Membership
--Set the is member flag for each date that is covered by a membership
UPDATE sd SET IsMember = 1
FROM #SignificantDates sd
JOIN #Membership m ON MembershipStartDate<= SignificantDate AND SignificantDate <= MembershipEndDate
--To demonstrate what we're about to do, Select all the dates and show the IsMember Flag and the previous value
SELECT sd.MemberID, sd.SignificantDate,sd.IsMember, prv.prevIsMember
FROM
#SignificantDates sd
JOIN (SELECT
MemberId,
SignificantDate,
IsMember,
Lag(IsMember,1) OVER (PARTITION BY MemberId ORDER BY SignificantDate desc) AS prevIsMember FROM #SignificantDates
) as prv
ON sd.MemberID = prv.MemberID
AND sd.SignificantDate = prv.SignificantDate
ORDER BY sd.MemberID, sd.SignificantDate
--Delete the ones where the flag is the same as the previous value
delete sd
FROM
#SignificantDates sd
JOIN (SELECT MemberId, SignificantDate,IsMember, Lag(IsMember,1) OVER (PARTITION BY MemberId ORDER BY SignificantDate) AS prevIsMember FROM #SignificantDates ) as prv
ON sd.MemberID = prv.MemberID
AND sd.SignificantDate = prv.SignificantDate
AND prv.IsMember = prv.prevIsMember
--SELECT the Start date for each period of membership and the day before the following period of non membership
SELECT
nxt.MemberId,
nxt.SignificantDate AS MembershipStartDate,
DATEADD(day,-1,nxt.NextSignificantDate) AS MembershipEndDate
FROM
(
SELECT
MemberID,
SignificantDate,
LEAd(SignificantDate,1) OVER (PARTITION BY MemberId ORDER BY SignificantDate) AS NextSignificantDate,
IsMember
FROM #SignificantDates
) nxt
WHERE nxt.IsMember = 1
I have a table containing the working hours for the company. We define the range of the dates and the number of hours for the range.
The number of working hours which is not in the defined range is 9.5 hours.
I want to have the non-defined range with the value of 9.5 added to the result record set.
how should I make the query to get this result?
The table definition and added records:
IF OBJECT_ID ('dbo.tbl_WorkingHours') IS NOT NULL
DROP TABLE dbo.tbl_WorkingHours
GO
CREATE TABLE dbo.tbl_WorkingHours
(
Startdate DATETIME NOT NULL,
EndDate DATETIME NOT NULL,
HoursDefined FLOAT NULL,
Description VARCHAR (255) NULL,
PRIMARY KEY (Startdate,EndDate)
)
INSERT INTO dbo.tbl_WorkingHours
(Startdate,EndDate,HoursDefined,Description)
VALUES
('3/4/2010','3/29/2010',7,'')
INSERT INTO dbo.tbl_WorkingHours
(Startdate,EndDate,HoursDefined,Description)
VALUES
('5/4/2010','5/29/2010',8,'')
The Result of Select * :
Startdate | EndDate | HoursDefined | Description
----------------------------------------------------
3/4/2010 3/29/2010 7
5/4/2010 5/29/2010 8
My desired record set:
Startdate | EndDate | HoursDefined | Description
----------------------------------------------------
1/1/1900 3/3/2010 9.5
3/4/2010 3/29/2010 7
3/30/2010 5/3/2010 9.5
5/4/2010 5/29/2010 8
5/30/2010 1/1/2050 9.5
The below assumes it is not possible to have overlapping ranges or two defined ranges that are contiguous but held as separate rows.
WITH wh AS
(
SELECT Startdate, EndDate, HoursDefined, Description,
ROW_NUMBER() over (order by startdate) as rn
FROM tbl_WorkingHours
)
SELECT Startdate, EndDate, HoursDefined, Description
FROM wh
UNION ALL
SELECT ISNULL(dateadd(day,1,w1.EndDate),'19000101'),
ISNULL(dateadd(day,-1,w2.StartDate),'20500101') , 9.5 AS HoursDefined,''
FROM wh w1 FULL OUTER JOIN wh w2
ON w2.rn = w1.rn+1
ORDER BY Startdate
try something like this:
DECLARE #tbl_WorkingHours table (Startdate DATETIME NOT NULL
,EndDate DATETIME NOT NULL
,HoursDefined FLOAT NULL
,Description VARCHAR (255) NULL
,PRIMARY KEY (Startdate,EndDate)
)
INSERT INTO #tbl_WorkingHours (Startdate,EndDate,HoursDefined,Description) VALUES ('3/4/2010','3/29/2010',7,'')
INSERT INTO #tbl_WorkingHours (Startdate,EndDate,HoursDefined,Description) VALUES ('5/4/2010','5/29/2010',8,'')
;WITH OrderedRows AS
( SELECT
Startdate,EndDate,HoursDefined,Description, ROW_NUMBER() OVER (ORDER BY Startdate,EndDate) AS RowNumber
FROM #tbl_WorkingHours
)
SELECT --before rows
CONVERT(datetime,'1/1/1900') AS Startdate,ISNULL(MIN(Startdate),CONVERT(datetime,'1/2/2050'))-1 AS EndDate,9.5 AS HoursDefined, CONVERT(VARCHAR (255),'') AS Description
FROM #tbl_WorkingHours
UNION ALL
SELECT --actual rows
Startdate,EndDate,HoursDefined,Description
FROM #tbl_WorkingHours
UNION ALL
SELECT --between rows
a.EndDate+1,b.Startdate-1,9.5,''
FROM OrderedRows a
INNER JOIN OrderedRows b ON a.RowNumber=b.RowNumber-1
UNION ALL
SELECT --after rows
ISNULL(MAX(EndDate),CONVERT(datetime,'1/1/2050')) AS Startdate,CONVERT(datetime,'1/2/2050')-1 AS EndDate,9.5 AS HoursDefined, CONVERT(VARCHAR (255),'') AS Description
FROM #tbl_WorkingHours
ORDER BY Startdate,EndDate
OUTPUT:
Startdate EndDate HoursDefined Description
----------------------- ----------------------- ---------------------- -----------
1900-01-01 00:00:00.000 2010-03-03 00:00:00.000 9.5
2010-03-04 00:00:00.000 2010-03-29 00:00:00.000 7
2010-03-30 00:00:00.000 2010-05-03 00:00:00.000 9.5
2010-05-04 00:00:00.000 2010-05-29 00:00:00.000 8
2010-05-29 00:00:00.000 2050-01-01 00:00:00.000 9.5
(5 row(s) affected)