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 |
|
frank.svs
Constraint Violating Yak Guru
368 Posts |
Posted - 2009-11-25 : 01:44:11
|
| Hi,Have a small requirement. I have a table with two columns and i require the below expected output.create table test1(c1 int,c2 int ) insert into test1 select 1,2 union allselect 2,3union allselect 6,8union allselect 7,7union allselect 8,8union allselect 4,9union allselect 9,9 select c1,c2 from test1 For the first row the value for c3 should start from 1.For the next record, we need compare c1 and c2 columns.The logic is as follows Case 1-------If the c1 == c2 then we need to retain the previous c3 value.Case 2 --------if c1 != c3 then we need to increment the previous value + 1 and display it as C3 column.Case 3-------a.In the first record itself C1 == C2 i.e equal then keep 0 for C3 column in the ouput.b. if the first record itself C1 ! = 2 then keep 1 as for C3 column in the ouput.For every record we need, keep the previous record intact.Expected output ===============C1 C2 C3 1 2 12 3 26 8 37 7 3 -- bcz C1 and C2 is equal8 8 3 -- bcz C1 and C2 is equal4 9 49 9 47 2 5 Any help would be greatly appreciated.Thanks in Advance. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-11-27 : 02:48:34
|
| [code]DECLARE @Cnt intSET @Cnt=0UPDATE tableSET @Cnt=C3=CASE WHEN C1 != C2 THEN @Cnt+1 ELSE @Cnt END[/code] |
 |
|
|
|
|
|