SQL 将已声明列中的值求和到总计中并按 id 分组
SQL SUM values in declared column into total and group by id
大家好互联网。
再次征求您的意见。
我有多个抄表器每 5 分钟收集一次电量并存储到 MS SQL 数据库中。
我执行 SQL 查询以从所有抄表器获取一天的数据,并检查每五分钟使用了多少电量并返回为 '
Consumed
' 结果。这是我用来获取数据的 SQL 语法:
SELECT Dep_Name, DoD,
Convert(FLOAT,T_Energy_A) - lag(T_Energy_A)
OVER(PARTITION BY Meter_ID ORDER BY DoD) AS Consumed
FROM Meter_Data
WHERE DoD>= DATEADD(HOUR,-1, GETDATE())
ORDER BY dateOfData DESC
我得到的结果是这样的:
Dep_Name DoD Consumed
Bath 6 2020-04-23 17:50:05.253 181
RPi Bath 5 2020-04-23 17:50:04.863 89
RPi Bath 4 2020-04-23 17:50:04.497 136
RPi Bath 3 2020-04-23 17:50:04.113 14
Solder Bath 6 2020-04-23 17:50:03.727 220
Solder Bath 5 2020-04-23 17:50:03.367 1261
Solder Bath 4 2020-04-23 17:50:02.997 409
Solder Bath 3 2020-04-23 17:50:02.590 0
Reflow 10 2020-04-23 17:50:02.287 440
Reflow 9 2020-04-23 17:50:02.197 244
Reflow 8 2020-04-23 17:50:02.020 361
Cam2 2020-04-23 17:50:01.787 180
Reflow 7 2020-04-23 17:50:01.457 366
Reflow 6 2020-04-23 17:50:01.030 506
Reflow 5 2020-04-23 17:50:00.837 453
Reflow 4 2020-04-23 17:50:00.770 280
Reflow 3 2020-04-23 17:50:00.697 243
Reflow 2 2020-04-23 17:50:00.630 389
Bath 6 2020-04-23 17:45:05.907 180
RPi Bath 5 2020-04-23 17:45:05.627 89
RPi Bath 4 2020-04-23 17:45:05.213 137
RPi Bath 3 2020-04-23 17:45:04.967 13
Solder Bath 6 2020-04-23 17:45:04.580 240
Solder Bath 5 2020-04-23 17:45:04.203 1254
Solder Bath 4 2020-04-23 17:45:03.967 413
Solder Bath 3 2020-04-23 17:45:03.807 0
Reflow 10 2020-04-23 17:45:03.560 493
Reflow 9 2020-04-23 17:45:03.163 245
Reflow 8 2020-04-23 17:45:02.807 360
Cam2 2020-04-23 17:45:02.433 179
Reflow 7 2020-04-23 17:45:02.063 379
Reflow 6 2020-04-23 17:45:01.473 505
Reflow 5 2020-04-23 17:45:01.383 450
Reflow 4 2020-04-23 17:45:01.157 279
Reflow 3 2020-04-23 17:45:00.780 242
Reflow 2 2020-04-23 17:45:00.657 387
Bath 6 2020-04-23 17:40:06.247 181
RPi Bath 5 2020-04-23 17:40:05.853 89
RPi Bath 4 2020-04-23 17:40:05.477 137
RPi Bath 3 2020-04-23 17:40:05.060 14
Solder Bath 6 2020-04-23 17:40:04.667 197
Solder Bath 5 2020-04-23 17:40:04.283 1212
Solder Bath 4 2020-04-23 17:40:03.887 385
Solder Bath 3 2020-04-23 17:40:03.547 0
Reflow 10 2020-04-23 17:40:03.280 495
Reflow 9 2020-04-23 17:40:03.063 244
Reflow 8 2020-04-23 17:40:02.600 361
Cam2 2020-04-23 17:40:02.303 178
Reflow 7 2020-04-23 17:40:01.943 353
Reflow 6 2020-04-23 17:40:01.500 508
Reflow 5 2020-04-23 17:40:01.280 449
Reflow 4 2020-04-23 17:40:01.040 280
Reflow 3 2020-04-23 17:40:00.687 241
Reflow 2 2020-04-23 17:40:00.620 387
Bath 6 2020-04-23 17:35:06.150 181
RPi Bath 5 2020-04-23 17:35:05.760 89
RPi Bath 4 2020-04-23 17:35:05.303 137
RPi Bath 3 2020-04-23 17:35:04.960 14
Solder Bath 6 2020-04-23 17:35:04.597 213
Solder Bath 5 2020-04-23 17:35:04.360 1178
现在我想将所有 'Consumed' 列数据汇总为每个部门的 totalPower,并以 1 天为间隔按 Dep_Name 分组?
像这样的东西:
Dep_Name totalPower
Bath 6 123
RPi Bath 5 321
RPi Bath 4 1566
RPi Bath 3 1134
Solder Bath 6 ...
Solder Bath 5 ...
Solder Bath 4 ...
... ...
可以吗?如果是,我该怎么做?
我是新手,所以我不知道。
如果T_Energy_A
一直在增加,你可以这样做:
select dep_name, max(t_energy_a) - min(t_energy_a) totalpower
from meter_data
where dod >= dateadd(hour,-1, getdate())
group by dep_name
如果您希望每天都这样做:
select cast(dod as date) dod_day, dep_name, max(t_energy_a) - min(t_energy_a) totalpower
from meter_data
group by cast(dod as date), dep_name
order by dod_day, dep_name
否则,您可以只向现有查询添加一级聚合:
select dep_name, cast(dod as date) dod_day, sum(consumed) totalpower
from (
select
dep_name,
dod,
t_energy_a - lag(t_energy_a) over(partition by meter_id order by dod) as consumed
from meter_data
) t
group by cast(dod as date), dep_name
order by dod_day, dep_name
大家好互联网。
再次征求您的意见。
我有多个抄表器每 5 分钟收集一次电量并存储到 MS SQL 数据库中。 我执行 SQL 查询以从所有抄表器获取一天的数据,并检查每五分钟使用了多少电量并返回为 '
Consumed
' 结果。这是我用来获取数据的 SQL 语法:
SELECT Dep_Name, DoD,
Convert(FLOAT,T_Energy_A) - lag(T_Energy_A)
OVER(PARTITION BY Meter_ID ORDER BY DoD) AS Consumed
FROM Meter_Data
WHERE DoD>= DATEADD(HOUR,-1, GETDATE())
ORDER BY dateOfData DESC
我得到的结果是这样的:
Dep_Name DoD Consumed
Bath 6 2020-04-23 17:50:05.253 181
RPi Bath 5 2020-04-23 17:50:04.863 89
RPi Bath 4 2020-04-23 17:50:04.497 136
RPi Bath 3 2020-04-23 17:50:04.113 14
Solder Bath 6 2020-04-23 17:50:03.727 220
Solder Bath 5 2020-04-23 17:50:03.367 1261
Solder Bath 4 2020-04-23 17:50:02.997 409
Solder Bath 3 2020-04-23 17:50:02.590 0
Reflow 10 2020-04-23 17:50:02.287 440
Reflow 9 2020-04-23 17:50:02.197 244
Reflow 8 2020-04-23 17:50:02.020 361
Cam2 2020-04-23 17:50:01.787 180
Reflow 7 2020-04-23 17:50:01.457 366
Reflow 6 2020-04-23 17:50:01.030 506
Reflow 5 2020-04-23 17:50:00.837 453
Reflow 4 2020-04-23 17:50:00.770 280
Reflow 3 2020-04-23 17:50:00.697 243
Reflow 2 2020-04-23 17:50:00.630 389
Bath 6 2020-04-23 17:45:05.907 180
RPi Bath 5 2020-04-23 17:45:05.627 89
RPi Bath 4 2020-04-23 17:45:05.213 137
RPi Bath 3 2020-04-23 17:45:04.967 13
Solder Bath 6 2020-04-23 17:45:04.580 240
Solder Bath 5 2020-04-23 17:45:04.203 1254
Solder Bath 4 2020-04-23 17:45:03.967 413
Solder Bath 3 2020-04-23 17:45:03.807 0
Reflow 10 2020-04-23 17:45:03.560 493
Reflow 9 2020-04-23 17:45:03.163 245
Reflow 8 2020-04-23 17:45:02.807 360
Cam2 2020-04-23 17:45:02.433 179
Reflow 7 2020-04-23 17:45:02.063 379
Reflow 6 2020-04-23 17:45:01.473 505
Reflow 5 2020-04-23 17:45:01.383 450
Reflow 4 2020-04-23 17:45:01.157 279
Reflow 3 2020-04-23 17:45:00.780 242
Reflow 2 2020-04-23 17:45:00.657 387
Bath 6 2020-04-23 17:40:06.247 181
RPi Bath 5 2020-04-23 17:40:05.853 89
RPi Bath 4 2020-04-23 17:40:05.477 137
RPi Bath 3 2020-04-23 17:40:05.060 14
Solder Bath 6 2020-04-23 17:40:04.667 197
Solder Bath 5 2020-04-23 17:40:04.283 1212
Solder Bath 4 2020-04-23 17:40:03.887 385
Solder Bath 3 2020-04-23 17:40:03.547 0
Reflow 10 2020-04-23 17:40:03.280 495
Reflow 9 2020-04-23 17:40:03.063 244
Reflow 8 2020-04-23 17:40:02.600 361
Cam2 2020-04-23 17:40:02.303 178
Reflow 7 2020-04-23 17:40:01.943 353
Reflow 6 2020-04-23 17:40:01.500 508
Reflow 5 2020-04-23 17:40:01.280 449
Reflow 4 2020-04-23 17:40:01.040 280
Reflow 3 2020-04-23 17:40:00.687 241
Reflow 2 2020-04-23 17:40:00.620 387
Bath 6 2020-04-23 17:35:06.150 181
RPi Bath 5 2020-04-23 17:35:05.760 89
RPi Bath 4 2020-04-23 17:35:05.303 137
RPi Bath 3 2020-04-23 17:35:04.960 14
Solder Bath 6 2020-04-23 17:35:04.597 213
Solder Bath 5 2020-04-23 17:35:04.360 1178
现在我想将所有 'Consumed' 列数据汇总为每个部门的 totalPower,并以 1 天为间隔按 Dep_Name 分组? 像这样的东西:
Dep_Name totalPower
Bath 6 123
RPi Bath 5 321
RPi Bath 4 1566
RPi Bath 3 1134
Solder Bath 6 ...
Solder Bath 5 ...
Solder Bath 4 ...
... ...
可以吗?如果是,我该怎么做?
我是新手,所以我不知道。
如果T_Energy_A
一直在增加,你可以这样做:
select dep_name, max(t_energy_a) - min(t_energy_a) totalpower
from meter_data
where dod >= dateadd(hour,-1, getdate())
group by dep_name
如果您希望每天都这样做:
select cast(dod as date) dod_day, dep_name, max(t_energy_a) - min(t_energy_a) totalpower
from meter_data
group by cast(dod as date), dep_name
order by dod_day, dep_name
否则,您可以只向现有查询添加一级聚合:
select dep_name, cast(dod as date) dod_day, sum(consumed) totalpower
from (
select
dep_name,
dod,
t_energy_a - lag(t_energy_a) over(partition by meter_id order by dod) as consumed
from meter_data
) t
group by cast(dod as date), dep_name
order by dod_day, dep_name