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 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-08-23 : 08:05:05
|
I have following data in a table variableNum Grp--- --- 1 --<-- 1 3 --<-- 2 5 --<-- 3 I want to update the Grp column to a number denoting which sequence group the number is in.This test dataNum Grp--- --- 1 --<-- 1 3 --<-- 2 4 --<-- 2 5 --<-- 2 should be updated as commented.I am completely stuck at the moment There will never be more than 200 rows in the table variable.Peter LarssonHelsingborg, Sweden |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-08-23 : 08:21:24
|
Need more coffee, Peso? I tend to lean towards this simple (and dirty) method...--datadeclare @t table (Num int primary key, Grp int)insert @t (Num) select 1 --<-- 1union all select 3 --<-- 2union all select 4 --<-- 2union all select 5 --<-- 2--calculationdeclare @Grp int, @Num intupdate @t set @Grp = case when Num = @Num + 1 then @Grp else isnull(@Grp, 0) + 1 end, @Num = Num, Grp = @Grpselect * from @t/*resultsNum Grp ----------- ----------- 1 13 24 25 2*/ Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-08-23 : 08:27:47
|
It is this approach I am stuck with (searched this site to find it).I am having a little bit of problem since the test data is not necessarily inserted in order.Num Grp--- --- 1 --<-- 1 3 --<-- 2 5 --<-- 2 4 --<-- 2 I must get som coffee soon.Peter LarssonHelsingborg, Sweden |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-08-23 : 09:09:33
|
Then, maybe this...--datadeclare @t table (Num int, Grp int)insert @t (Num) select 1 --<-- 1union all select 3 --<-- 2union all select 9 --<-- 4union all select 5 --<-- 2union all select 4 --<-- 2union all select 7 --<-- 3--calculationdeclare @u table (Grp int identity(1, 1) primary key, Num int) --group start Numsinsert @u select Num from @t a where not exists (select Num from @t where Num = a.Num - 1) order by Numdeclare @v table (Grp int identity(1, 1) primary key, Num int) --group end Numsinsert @v select Num from @t a where not exists (select Num from @t where Num = a.Num + 1) order by Numupdate t set Grp = u.Grp from @u u inner join @v v on u.Grp = v.Grp inner join @t t on t.Num between u.Num and v.Numselect * from @t/*resultsNum Grp ----------- ----------- 1 13 29 45 24 27 3*/ Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2006-08-23 : 09:26:47
|
or just for fun... no variables or updates...declare @t table (Num int, Grp int)insert @t (Num) select 1 --<-- 1union all select 3 --<-- 2union all select 9 --<-- 4union all select 5 --<-- 2union all select 4 --<-- 2union all select 7 --<-- 3--Select Z.Num, count(Y.Num)From @t ZInner Join ( Select A.Num From @t A Left Join @t B On A.num-1 = B.num Where B.num is null ) YOn Z.Num >= Y.NumGroup By Z.Num Corey Co-worker on children "...when I have children, I'm going to beat them. Not because their bad, but becuase I think it would be fun ..." |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-08-24 : 02:52:43
|
| Excellent Corey.Thank you Ryan for your efforts!Peter LarssonHelsingborg, Sweden |
 |
|
|
|
|
|
|
|