Can someone help me come up with a SQL query to do the following
Order a table by individual pairs of rows, where the pairs are consecutive rows based on timestamp. Then order all of these pairs by the average of an attribute of both rows in given pair. Select the first pair / two rows.
thanks. ive been puzzling over it for while and am not sure where to start.
Example:
[Timestamp] [Views]
1 23
2 17
3 30
4 5
5 47
6 15
Order pairs(timestamp) by SUM(views) ASC
[Timestamp] [Views] [(sum)]
3 30 35
4 5
1 23 40
2 17
5 47 62
6 15
If the
timestampfield is always in a sequence and has no gaps, this query should work:Otherwise, you need to copy the results to a temp table with a autoincrement field to generate the full sequence.