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 2005 Forums
 Transact-SQL (2005)
 Create Multiple records from single record

Author  Topic 

ambily
Starting Member

4 Posts

Posted - 2010-02-16 : 08:51:11
I have a table with start and end date adn duration fro recroding the leave taken by employees.

Currently for a 2 day leave, the table holds a start and end date and duration as 2 days.

I want to generate a report on a day by day basis.ie 2 records for above case.Basically trying to generate multiple records from single record based on duration(datediff between start and end date).
Is it possible to do this without looping through recrod and splitting?

Please help me.

Sachin.Nand

2937 Posts

Posted - 2010-02-16 : 09:34:02
Something like this

declare @tbl as table(id int identity(1,1),leavestartdate datetime,leaveenddate datetime)
insert into @tbl
select '10-jan-2010','15-jan-2010' union all
select '20-jan-2010','22-jan-2010' union all
select '5-feb-2010','10-feb-2010'
;with cte
as
(
select id,leavestartdate,leaveenddate,leavestartdate as leavedates from @tbl t1
union all
select t2.id,t2.leavestartdate,t2.leaveenddate,c1.leavedates+1 from @tbl t2
inner join cte c1 on c1.leavedates+1<=t2.leaveenddate and c1.id=t2.id
)

select id,leavedates
from cte order by id --- ur where condition will come here
option (maxrecursion 0)




PBUH
Go to Top of Page

ambily
Starting Member

4 Posts

Posted - 2010-02-16 : 10:48:33
Thanks for the immediate response.
Sorry that i didnt mention the version.I am using SQL 2000 server but 2005 interface and is not accepting the cte.

I am trying to replace the cte with corresponding sql 2000 feature.

Please let me know if you could help with any available solution in SQL 2000 solution.

Thanks
Ambily
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-02-16 : 11:43:43
quote:
Originally posted by ambily

Thanks for the immediate response.
Sorry that i didnt mention the version.I am using SQL 2000 server but 2005 interface and is not accepting the cte.

I am trying to replace the cte with corresponding sql 2000 feature.

Please let me know if you could help with any available solution in SQL 2000 solution.

Thanks
Ambily



Sorry was on the way home.

declare @tbl as table(id int identity(1,1),leavestartdate datetime,leaveenddate datetime)
insert into @tbl
select '10-jan-2010','15-jan-2010' union all
select '20-jan-2010','22-jan-2010' union all
select '5-feb-2009','10-feb-2009'

select id,dates from
(
select id,leavestartdate,leaveenddate, DATEADD(dd,datediff(dd,0,m1.number),leavestartdate)as dates from master.dbo.spt_values m1
left join @tbl t1 on t1.leavestartdate<=DATEADD(dd,datediff(dd,0,m1.number),leavestartdate)
where type='p'

)t where dates between leavestartdate and leaveenddate


PBUH
Go to Top of Page

ambily
Starting Member

4 Posts

Posted - 2010-02-16 : 11:44:26
That query works exactly how i want.But i am unable to convert it to SQL 2000 version.

Hope someone can help me out.

Thanks
Go to Top of Page

ambily
Starting Member

4 Posts

Posted - 2010-02-16 : 11:45:57
Wow.Thanks lot.
That solution made my day. :)

Thank you once again.
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-02-16 : 11:48:21
quote:
Originally posted by ambily

Wow.Thanks lot.
That solution made my day. :)

Thank you once again.



You are most welcome .

PBUH
Go to Top of Page
   

- Advertisement -