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 |
rgombina
Constraint Violating Yak Guru
319 Posts |
Posted - 2008-12-02 : 08:26:09
|
Hi, another basic question. I would like to loop records but reset counter if grouped column changes.ID Col11 chevy1 chevy2 ford2 ford3 toyota3 toyota3 toyotaDECLARE @counter intDECLARE @_ID intDECLARE @currID intDECLARE @prevID intSET @counter = 0SELECT @_ID = ID FROM @TableSET @currID = @_IDIF @currID <> @prevID BEGIN -- do process SET @prevID = @currID SET @counter = @counter + 1 ENDELSE BEGIN SET @counter = 0 END Am I in the right direction? Thanks. |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-12-02 : 08:31:00
|
[code]DECLARE @Sample TABLE ( ID INT PRIMARY KEY CLUSTERED, Col1 VARCHAR(20), grp INT )INSERT @Sample ( ID, Col1 )SELECT 1, 'chevy' UNION ALLSELECT 2, 'chevy' UNION ALLSELECT 3, 'ford' UNION ALLSELECT 4, 'ford' UNION ALLSELECT 5, 'toyota' UNION ALLSELECT 6, 'toyota' UNION ALLSELECT 7, 'toyota'SELECT *FROM @SampleDECLARE @Col1 VARCHAR(20), @grp INTUPDATE @SampleSET @grp = grp = CASE WHEN @Col1 = Col1 THEN @grp ELSE COALESCE(@grp, 0) + 1 END, @Col1 = Col1SELECT *FROM @Sample[/code] E 12°55'05.63"N 56°04'39.26" |
|
|
rgombina
Constraint Violating Yak Guru
319 Posts |
Posted - 2008-12-02 : 08:46:04
|
Thanks Peso!!! |
|
|
|
|
|
|
|