Give Row number to each row ( by using ROW_NUMBER) , then divide this row_number by a particular no say 10 to get grp no.
( Note that this 10 can be any number , it will be the group or page size)
select fuellevel , DIFF , timeparts , row_number() over ( order by timeparts ) as rownum , row_number() over ( order by timeparts ) /10 as grp from RSCFuelChart
e.g
fuellevel | DIFF | timeparts | rownum | grp |
1.02 | 0 | 7:20:17 | 1 | 0 |
1.02 | 0 | 7:20:50 | 2 | 0 |
1.02 | 0 | 7:21:20 | 3 | 0 |
1.02 | 0 | 7:21:50 | 4 | 0 |
1.02 | 0 | 7:22:20 | 5 | 0 |
1.02 | 0 | 7:22:50 | 6 | 0 |
1.02 | 0 | 7:23:20 | 7 | 0 |
1.02 | 0 | 7:23:50 | 8 | 0 |
1.02 | 0 | 7:24:18 | 9 | 0 |
1.02 | 0 | 7:24:51 | 10 | 1 |
1.02 | 0 | 7:25:21 | 11 | 1 |
1.02 | 0 | 7:25:51 | 12 | 1 |
1.02 | 0 | 7:26:21 | 13 | 1 |
1.02 | 0 | 7:26:51 | 14 | 1 |
1.02 | 0 | 7:27:21 | 15 | 1 |
1.02 | 0 | 7:27:48 | 16 | 1 |
1.02 | 0 | 7:28:22 | 17 | 1 |
1.02 | 0 | 7:28:52 | 18 | 1 |
1.02 | 0 | 7:29:22 | 19 | 1 |
1.02 | 0 | 7:29:52 | 20 | 2 |
1.02 | 0 | 7:30:22 | 21 | 2 |
1.02 | 0 | 7:30:52 | 22 | 2 |
1.02 | 0 | 7:30:52 | 23 | 2 |
1.02 | 0 | 7:31:22 | 24 | 2 |
1.02 | 0 | 7:31:53 | 25 | 2 |
1.02 | 0 | 7:32:23 | 26 | 2 |
1.02 | 0 | 7:32:53 | 27 | 2 |
1.02 | 0 | 7:33:23 | 28 | 2 |
1.02 | 0 | 7:33:53 | 29 | 2 |
1.02 | 0 | 7:34:23 | 30 | 3 |
1.02 | 0 | 7:34:53 | 31 | 3 |
1.02 | 0 | 7:35:21 | 32 | 3 |
1.02 | 0 | 7:35:54 | 33 | 3 |
1.02 | 0 | 7:36:24 | 34 | 3 |
This can be very useful in smoothning the graphs, e.g. following query takes avg of every 10 records and max(time) of these 10 records.
select AVG(a.fuellevel) as fiellevel, MAX(timeparts) as timeparts from ( select fuellevel , DIFF , timeparts , row_number() over ( order by timeparts ) as rownum , row_number() over ( order by timeparts ) /10 as grp from RSCFuelChart ) A group
this essentially reduces no of recs from say 500 in all to 50 only to be plotted. by a.grp
==============================
So to summarize :
declare @pagesize int = 20 , @pagenumber int = 1select * from ( select fuellevel , DIFF , timeparts , row_number() over ( order by timeparts ) as rownum from RSCFuelChart ) A where ((a.rownum /@pagesize) + 1) = @pagenumber
No comments:
Post a Comment