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 |
|
ayamas
Aged Yak Warrior
552 Posts |
Posted - 2008-06-22 : 06:18:37
|
| Hi Guys,I have a resulset in this manner.id----- amt----------- -----------1----- NULL2----- 203----- 204----- 208----- NULL9----- 3010----- 12011----- 11021----- NULL22----- 110Furthermore I would like to sum the minimum amt & maximum amt between two Null values.So ideally the output should beminid maxid amt2----- 4----- 409----- 11----- 14021---- NULL----- 110Thanks for any help. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-22 : 06:42:32
|
| [code];With Base_CTE (RowNo,ID) AS(SELECT ROW_NUMBER() OVER(ORDER BY id) AS RowNo, idFROM YourTableWHERE amt IS NULL)SELECT min.id as minid,max.id as maxid,min.amt+max.amt as amtFROM Base_CTE bLEFT JOIN Base_CTE cON c.RowNo=b.RowNo + 1OUTER APPLY (SELECT TOP 1 id,amt FROM YourTable WHERE id > b.id AND (id < c.id OR c.id IS NULL) AND amt IS NOT NULL ORDER BY id )minOUTER APPLY (SELECT TOP 1 id,amt FROM YourTable WHERE id > b.id AND (id < c.id OR c.id IS NULL) AND amt IS NOT NULL ORDER BY id DESC )max[/code] |
 |
|
|
|
|
|