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.
Author |
Topic |
Nil35
Starting Member
20 Posts |
Posted - 2013-07-17 : 11:54:56
|
CREATE TABLE #t1( ID int, COL1 int, COL2 int)INSERT INTO #t1SELECT 0,0,NULLUNION ALLSELECT 1,3,1UNION ALLSELECT 2,5,3UNION ALLSELECT 3,4,2UNION ALLSELECT 4,2,1SELECT A.*,A.COL1 + B.COL2 AS COLAB FROM #T1 aLEFT JOIN #T1 B ON A.ID = B.ID +1DROP TABLE #t1USNING SQL SERVER 2008R2ANY ALTERNATIVE CODE FOR THIS PLEASE HELP ME OUT THANKSnil |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-17 : 12:04:36
|
Why whats the issue with current code?SELECT t1.*,t1.COL1 + t2.COL2 AS COLABFROM #t1 t1OUTER APPLY(SELECT COL2 FROM #t1 WHERE ID = t1.ID -1 )t2 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
Nil35
Starting Member
20 Posts |
Posted - 2013-07-17 : 12:09:53
|
THANKS VISAKHnil |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-17 : 13:21:28
|
This is another methodSELECT MAX(CASE WHEN n=0 THEN ID END) AS ID, MAX(CASE WHEN n=0 THEN COL1 END) AS COl1, MAX(CASE WHEN n=0 THEN COL2 END) AS COl2, MAX(CASE WHEN n=0 THEN COL1 END) + MAX(CASE WHEN n=1 THEN COL2 END) AS COLABFROM #t1 t1CROSS JOIN (VALUES(0),(1)) AS t2(n)GROUP BY t1.ID + t2.n HAVING MAX(CASE WHEN n=0 THEN ID END) IS NOT NULL ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|