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 |
|
slimt_slimt
Aged Yak Warrior
746 Posts |
Posted - 2009-01-15 : 10:52:05
|
hii have following code:declare @v1 int;declare @sub int;set @v1 = 1;set @sub = 3;declare @temp table(val int,ord int)insert into @tempselect 123, 1 union allselect 323, 2 union allselect 645, 3 union allselect 412, 4 union allselect 654, 5 union allselect 745, 6 union allselect 812, 7 union allselect 954, 8 select * from @tempdeclare @i int;set @i = 0;select @i = count(*) from @tempdeclare @step int;set @step = 0;set @step = (@i / 3);declare @intg int;set @intg = 0;while @intg-1 <= @stepbegin insert into new_table select min(val) as min_val ,max(val) as max_val from @temp where ord between @v1 and @sub set @intg = @intg + 1 set @v1 = @v1 + @sub set @sub= @sub + @subend; it should get results into groups by 3 numbersval from 1st till 3rd4th till 6th7th till ...endand for each of this group i should have min value and max value.but the loop does not work the way it should :)any ideas? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-15 : 23:49:19
|
you dont need a loop. just useselect min(val),max(val)from @tempgroup by (ord-1)/3 |
 |
|
|
slimt_slimt
Aged Yak Warrior
746 Posts |
Posted - 2009-01-16 : 01:52:11
|
visakh16, thank you. but i've found the "bug".i'm posting code again, which is now working perfect :)bestdeclare @temp table(val int,ord int)insert into @tempselect 123, 1 union allselect 323, 2 union allselect 645, 3 union allselect 412, 4 union allselect 654, 5 union allselect 745, 6 union allselect 812, 7 union allselect 954, 8 union allselect 12, 9 union allselect 2354, 10 union allselect 1245, 11 union allselect 5312, 12 union allselect 954, 13 union allselect 454, 14 union allselect 654, 15 union allselect 754, 16 select * into #tk_temp from @tempdeclare @v1 int;declare @sub int;set @v1 = 1;set @sub = 3;declare @i int;set @i = 0;select @i = count(*) from #t_tempprint @ideclare @step int;set @step = 0;set @step = (@i / 3);declare @intg int;set @intg = 0;while @intg-1 <= @stepbegin insert into t_table select min(val) as min_val ,max(val) as max_val from #t_temp where ord between @v1 and @sub set @intg = @intg + 1--at this step definition there was error in previous post set @v1 = 1 + @sub--also here :) set @sub= 3 + @subend;select * from #t_tempselect * from t_table |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-16 : 02:48:43
|
| what was problem with my suggestion? why still use loop? |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-01-16 : 07:24:36
|
| << but i've found the "bug".>>What is it?MadhivananFailing to plan is Planning to fail |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-01-16 : 07:37:36
|
Maybe values in ord column is not sequential?DECLARE @Sample TABLE ( Val INT, Ord INT )INSERT @SampleSELECT 123, 1 UNION ALLSELECT 323, 3 UNION ALLSELECT 645, 5 UNION ALLSELECT 412, 6 UNION ALLSELECT 654, 7 UNION ALLSELECT 745, 8 UNION ALLSELECT 812, 9 UNION ALLSELECT 954, 10 UNION ALLSELECT 12, 15 UNION ALLSELECT 2354, 16 UNION ALLSELECT 1245, 20 UNION ALLSELECT 5312, 21 UNION ALLSELECT 954, 30 UNION ALLSELECT 454, 40 UNION ALLSELECT 654, 41 UNION ALLSELECT 754, 49 SELECT grp, MIN(Val) AS Lowest, MAX(Val) AS HighestFROM ( SELECT Val, 1 + (ROW_NUMBER() OVER (ORDER BY Ord) - 1) / 3 AS grp FROM @Sample ) AS dGROUP BY grpORDER BY grp E 12°55'05.63"N 56°04'39.26" |
 |
|
|
|
|
|
|
|