Here is my scenario:
I have a single table with 2 columns. ID and Value. ID is int and value is real.
ID Value
1 6.7
2 8.9
3 4.5
5 3.2
8 2.5
9 2.1
10 1.0
15 2.3
18 2.4
19 4.0
20 3.2
I would like to compose a SP that receives a grouping number (Group) and an operation (Op) and returns a new table in the following manner:
Group = 2,
Op = Max
IDstart IDend Value
1 2 8.9
3 5 4.5
8 9 2.5
10 15 2.3
18 19 4.0
20 20 3.2
Group = 3,
Op = Min
IDstart IDend Value
1 3 4.5
5 9 2.1
10 18 1.0
19 20 3.2
Group defines how many rows to combine into a single row (in the new table) and operation defines what operation to do on the group of rows, the operations I need are maximum, minimum and average.
The last group may contain less rows than all the rest of the groups. if the last group has a single value IDstart = IDEnd. ID is unique but may have ‘gaps’.
I’m looking for the fastest way to do this, any help will be appreciated.
Using SQL Server 2008 R2
Gilad.
Reasoning goes like this
ROW_NUMBER()function and some arithmetic allows you to create a dummy column placing each ID in a group of the size you specify.CASEstatement. Should you need additional operators, you would only have to expand thisCASEstatement.Script