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 |
|
sunny_mash
Starting Member
2 Posts |
Posted - 2010-03-12 : 14:25:52
|
| Is it possible that i can group data for every nth record.I have a table with the following structureDateTime | Value2010-01-01 00:08:25.000 | 52010-01-01 00:08:55.000 | 62010-01-01 00:06:55.000 | 82010-01-01 00:07:25.000 | 32010-01-01 00:07:55.000 | 72010-01-01 00:09:25.000 | 22010-01-01 00:13:25.000 | 32010-01-01 00:13:55.000 | 42010-01-01 00:14:25.000 | 22010-01-01 00:14:55.000 | 3 I want to now group the data based on 2 records each and have the min and max value. The output need to appear like this (for the DateTime column, i only need to consider the second value)Output Required DateTime | Min |Max2010-01-01 00:08:55.000 | 5 | 62010-01-01 00:07:25.000 | 3 | 82010-01-01 00:09:25.000 | 2 | 72010-01-01 00:13:55.000 | 3 | 42010-01-01 00:14:55.000 | 2 | 3 Can this be achieved via a single SQL Statement?Thanks in advance for the help? |
|
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2010-03-12 : 16:45:05
|
| [CODE]declare @tbl1 table ( row int, dt datetime, value int )INSERT INTO @tbl1SELECT 1, '2010-01-01 00:08:25.000', 5 union allSELECT 2, '2010-01-01 00:08:55.000', 6 union allSELECT 3, '2010-01-01 00:06:55.000', 8 union allSELECT 4, '2010-01-01 00:07:25.000', 3 union allSELECT 5, '2010-01-01 00:07:55.000', 7 union allSELECT 6, '2010-01-01 00:09:25.000', 2 union allSELECT 7, '2010-01-01 00:13:25.000', 3 union allSELECT 8, '2010-01-01 00:13:55.000', 4 union allSELECT 9, '2010-01-01 00:14:25.000', 2 union allSELECT 10, '2010-01-01 00:14:55.000', 3 select distinct max(dt) over(partition by (row - 1) / 2) dt, min(value) over(partition by (row - 1) / 2) minVal, max(value) over(partition by (row - 1) / 2) maxValfrom @tbl1order by dt[/CODE]Change the "2" to be your grouping size.=======================================There are no passengers on spaceship earth. We are all crew. -Marshall McLuhan, educator and philosopher (1911-1980) |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-03-13 : 01:11:04
|
| [code]declare @tbl1 table ( row int, dt datetime, value int )INSERT INTO @tbl1SELECT 1, '2010-01-01 00:08:25.000', 5 union allSELECT 2, '2010-01-01 00:08:55.000', 6 union allSELECT 3, '2010-01-01 00:06:55.000', 8 union allSELECT 4, '2010-01-01 00:07:25.000', 3 union allSELECT 5, '2010-01-01 00:07:55.000', 7 union allSELECT 6, '2010-01-01 00:09:25.000', 2 union allSELECT 7, '2010-01-01 00:13:25.000', 3 union allSELECT 8, '2010-01-01 00:13:55.000', 4 union allSELECT 9, '2010-01-01 00:14:25.000', 2 union allSELECT 10, '2010-01-01 00:14:55.000', 3 select dt,minvalue,maxvalue from(select *,minvalue=(select min(value)from @tbl1 t1 where t2.row between t1.row and t1.row+1having COUNT(*)=2),maxvalue=(select max(value)from @tbl1 t1 where t2.row between t1.row and t1.row+1having COUNT(*)=2)from @tbl1 t2)t where (row%2)=0[/code]PBUH |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-13 : 01:39:08
|
| [code]declare @N int,@Grp intSELECT @N=2,-- number of records by which you need to group@Grp=(COUNT(1) OVER ())/@NFROM @tbl1SELECT GrpNo,MAX(dt),MIN(value),MAX(value)FROM (SELECT NTILE(@Grp) OVER (ORDER BY dt) AS GrpNo,dt,value FROM @tbl1)tGROUP BY GrpNo[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
sunny_mash
Starting Member
2 Posts |
Posted - 2010-03-13 : 05:50:33
|
Thanks guys, all the solutions work. You have made my day |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-03-13 : 05:57:36
|
| You are welcomePBUH |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-13 : 10:56:46
|
| welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|