Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Splitting a row into multiple rows

Author  Topic 

mattisimo
Starting Member

3 Posts

Posted - 2008-09-10 : 06:08:57
Hi, I am trying to do the following and am not sure of the best approach. Any guidance would be greatly appreciated.

I have a table with several fields including
ID | StartDateTime | EndDateTime

The values in the start and end fields could be anything from a few seconds apart to over 2 years. Format is dd/mm/yy.

I need to be able to take a row and split it into multiple rows based on the start and end dates so that each row is for a single day. e.g. Row = ID : 1 | Start : 01/01/08 13:00:00 | End : 03/01/08 13:00:00
This row starts on 1st Jan 1pm and ends 3rd Jan 1pm. I would need to create the following rows in the result of the SQL.

ID : 1 | Start : 01/01/08 13:00:00 | End : 01/01/08 23:59:50
ID : 1 | Start : 02/01/08 00:00:00 | End : 02/01/08 23:59:50
ID : 1 | Start : 03/01/08 00:00:00 | End : 03/01/08 13:00:00

I am not able to create any temp tables in the database, so if these are needed I will need to use an interim database for analysis. This is because it is a 3rd party DB and I'm not allowed to modify it.

Is it possible to do something like this in a single query or would I need to use temp tables?

Many thanks for any guidance,

Matt

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-09-10 : 08:25:29
see this thread http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=42516 and employ similar technique there


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

mattisimo
Starting Member

3 Posts

Posted - 2008-09-10 : 09:21:15
Hi KH,

Many thanks for your post. I'll have a read and try to get my head around that solution.

Matt
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-09-10 : 09:40:58
There are some 2000 based solutions to the recent "continuation" of that thread here (even though it's in a 2005 forum):
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=110247

Be One with the Optimizer
TG
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-09-10 : 10:30:50
Since my head was already in this solution I took the 2000 example I did (TKizer called it TG1) and modified to use your structure. In case you haven't come accoss this concept before, many solutions that involve generating rows use a "numbers" table or "tally" table. It is simply a pre-existing table usually a single integer value set as a clustered index. In this example the numbers table is a table variable.

set nocount on

--Set up your Table
set dateformat 'dmy'
declare @t table ([id] int, [start] datetime, [end] datetime)
insert @t
select 1,'01/01/08 13:00:00', '03/01/08 13:00:00'

--set up a table of Integers (starting with 0)
declare @numbers table (number int)
insert @numbers select number from master..spt_values where type = 'p'


select d.id
,case
when n.number = 0 then d.[start]
else dateadd(day, n.number, dateadd(day, datediff(day, 0, d.[start]), 0))
end as [start]
,case
when d.SplitCount = 0 then [end]
when n.number = 0 then dateadd(minute, datediff(minute, d.[start], d.startDayEnd)-1, d.[start])
when n.number < d.SplitCount then dateadd(minute, n.number*1439, d.startDayEnd)
else d.[end]
end as [end]
from (
select t.id
,t.[start]
,t.[end]
,dateadd(day, 1, datediff(day, 0, t.[start])) startDayEnd
,dateadd(day, 0, datediff(day, 0, t.[end])) endDayStart
,datediff(day, t.[start], t.[end]) SplitCount
from @t t
) d
inner join @numbers n
on n.number <= d.SplitCount


OUTPUT:
id start end
----------- ----------------------- -----------------------
1 2008-01-01 13:00:00.000 2008-01-01 23:59:00.000
1 2008-01-02 00:00:00.000 2008-01-02 23:59:00.000
1 2008-01-03 00:00:00.000 2008-01-03 13:00:00.000


Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -