简体   繁体   中英

SQL Difference between two rows with group by

Table: rawdata

+---------------------+--------+-------+
|        time         | device | value |
+---------------------+--------+-------+
| 2019-07-19 12:50:00 | a      |   400 |
| 2019-07-19 12:50:00 | a      |   900 |
| 2019-07-19 12:50:00 | a      |   450 |
| 2019-07-19 12:50:00 | b      |   300 |
| 2019-07-19 12:50:00 | b      |   250 |
| 2019-07-19 12:39:00 | a      |   200 |
| 2019-07-19 12:39:00 | a      |   125 |
| 2019-07-19 12:45:00 | b      |   165 |
| 2019-07-19 12:30:00 | a      |    40 |
| 2019-07-19 12:30:00 | a      |    35 |
+---------------------+--------+-------+

Result:

+---------------------+--------+--------+------------------------------+
|        time         | device |  value |                              |
+---------------------+--------+--------+------------------------------+
| 2019-07-19 12:50:00 | a      |    700 | see calculation 1 for detail |
| 2019-07-19 12:50:00 | b      |    135 | see calculation 2 for detail |
| 2019-07-19 12:45:00 | b      |    165 | see calculation 3 for detail |
| 2019-07-19 12:39:00 | a      |    160 | see calculation 4 for detail |
| 2019-07-19 12:30:00 | a      |     40 | see calculation 5 for detail |
+---------------------+--------+--------+------------------------------+

calculation: first group by device and time, and find max value of that group. Then find max value of previuos record of same device in group and substract these two max value

1st record in group by result would be 12:50:00 and 'a' Max value of 'a' in 12:50 group = 900, Previous Record of 'a' (or next in list) = 12:39 and its max value = 200

so value= 900-200= 700

2nd record in group by result would be 12:50:00 and 'b' Max value of 'b' in 12:50:00 group = 300, Previous Record of 'b' (or next in list) = 12:45:00 and its max value = 165

So value= 300-165= 135

3rd record in group by result would be 12:45:00 and 'b' Max value of 'b' in 12:45:00 group = 165, Previous Record of 'b' (or next in list) = null and its max value = null (0)

So value= 165-0= 165

4th record in group by result would be 12:39:00 and 'a' Max value of 'a' in 12:39:00 group = 200, Previous Record of 'a' (or next in list) = 12:30:00 and its max value = 40

So value= 200-40= 160

5th record in group by result would be 12:30:00 and 'a' Max value of 'a' in 12:30:00 group = 40, Previous Record of 'a' (or next in list) = null and its max value = null (0)

So value= 40-0= 40

Please help me with mssql query for this.

I think this is aggregation with lag() :

select time, device, max(value) as max_value,
       (max(value) - lag(max(value)) over (partition by device order by time)) as diff
from rawdata
group by time, device
order by time desc;

Here is a db<>fiddle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM