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
 General SQL Server Forums
 Database Design and Application Architecture
 Need help on Composite Query

Author  Topic 

ashdesai
Starting Member

1 Post

Posted - 2011-07-01 : 03:59:28
I have a table which is as follows

Act Code R.code Start date
15239 8484 27-JUN-11
15239 8484 27-DEC-11
15239 8484 29-DEC-11

Now i need a query which extracts these 3 columns along with a 4th column which is end date.This end date is the start date of next row.

For eg 27-DEC-11 is the end date of first row in the table.

How do i do it?

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2011-07-01 : 04:12:30
Use CROSS/OUTER APPLY.

SELECT t.[Act Code], t.[R.code], t.[Start date], f.nextDate
FROM dbo.Table1 AS t
OUTER APPLY (SELECT MIN(w.[Start date]) FROM dbo.Table1 AS w WHERE w.[Act Code],= t.[Act Code] AND w.[R.code] = t.[R.code] AND w.[Start date] > t.[Start date]) AS f(nextDate)


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page
   

- Advertisement -