|
Hello, I am using IBM DB2, I have a table which gives me following data: START_DATE/END_DATE/LEAVE_NAME 08-01-2011/08-03-2011/CASUAL LEAVE I want the output as: START_DATE/END_DATE/LEAVE_NAME/date 08-01-2011/08-03-2011/CASUAL LEAVE/08-01-2011 08-01-2011/08-03-2011/CASUAL LEAVE/08-02-2011 08-01-2011/08-03-2011/CASUAL LEAVE/08-03-2011 I want one extra date column which gives me date from the start and end date. The StartDate and EndDate column are of "date" datatype. Plz help!! I m using IBM DB2.
(comments are locked)
|
|
Whilst @Håkan Winther is right in that this is a MS-SQL Server site, maybe we can still point you in the right direction for the solution. What you need to do is expand the days between start and finish. This can be done using a numbers or tally table. I have managed this through the use of a common table expression (the code starting with "WITH"). This just generates a list of integers, starting with 0. I then multiply the original data using the inner join, driving the join by calculating the difference in days between the start and end of leave entry. In your example the leave is 3 days, so three rows are generated. I then create a final column that turns the row number into the actual date that the row represents. Take a look at the code, I have no idea how it would translate to DB2, but I think the general idea should be translatable. +1 for the SQL Server solution. :-)
Aug 09 '11 at 02:05 AM
Pavel Pawlowski
Thanks @Pavel! It should be portable, not in the sense of copy-paste, but the general idea should work.
Aug 09 '11 at 02:18 AM
WilliamD
+1. But I would use a #temp table instead of a CTE, to be able to add a clustered index on the tally table. That makes quite a difference to the performance.
Aug 09 '11 at 02:22 AM
Magnus Ahlkvist
You're right - DB2 is a mystery to us all :)
Aug 09 '11 at 02:32 AM
Magnus Ahlkvist
(comments are locked)
|


have you tried www.stackoverflow.com