正在根据时间戳、ID 和单位对 SQL 查询进行排序
Sorting SQL query from timestamp, id and Unit
(SQL 服务器 14.0.3421.10 使用 Microsoft SQL 服务器 )
我知道你们总是乐于助人,在这种情况下我什至不知道如何 google 因为 SQL 不是我的菜。代码如下
declare @refdt as datetime
declare @loaddt as datetime
set @refdt='2022-04-01 01:00:00:000'
set @loaddt='2022-04-01 02:00:00:000'
select dt, id, Unit, MIN(pmVoltage) as[test]
from energymeter15min
where (id LIKE '3_4237') and (dt BETWEEN @refdt and @loaddt)
group by dt, Unit, pmVoltage, id
order by Unit, dt
此查询的结果如下所示
但我只是想在@refdt 和@loaddt 之间的时间跨度内显示 pmVoltage 的最小值
像这样
尝试从 GROUP BY 中取出 pmVoltage - 最小值由 group by 确定,不应包含在其中。
group by dt, Unit, id
order by Unit, dt
试一试,如果有效请告诉我。对于具有最低 pmVoltage 值的每个 Unit/id 组合,它应该只显示一条记录。
declare @refdt as datetime;
declare @loaddt as datetime;
set @refdt='2022-04-01 01:00:00:000';
set @loaddt='2022-04-01 02:00:00:000';
With data AS(
select dt, id, Unit, pmVoltage test
, ROW_NUMBER() OVER(Partition BY Unit, id ORDER BY pmVoltage) rowNum
from energymeter15min
where id LIKE '3_4237'
and dt BETWEEN @refdt and @loaddt
)
select dt, id, Unit, test
from data
where rowNum = 1
我选择了完全不同的设置
declare @refdt as datetime ='2022-04-06 01:00:00:000'
declare @loaddt as datetime ='2022-04-06 04:00:00:000'
declare @id as int =330647
select min(dt) as startdt, max(dt) as stopdt,
energymeter15min.id,
Unit,
min(pmVoltage) as minVoltage,
max(pmVoltage) as maxVoltage,
min(pmMinPowerConsumption)as minPowerConsumption,
max(pmMaxPowerConsumption) as maxPowerConsumption
from energymeter15min
join fruname on energymeter15min.frunameid = fruname.id
where (dt between @refdt and @loaddt )
and right(energymeter15min.id,4) in ( right(@id,4) )
and Unit <>'BB-1'
group by energymeter15min.id,Unit
order by Unit asc
结果是否呈现为
(SQL 服务器 14.0.3421.10 使用 Microsoft SQL 服务器 ) 我知道你们总是乐于助人,在这种情况下我什至不知道如何 google 因为 SQL 不是我的菜。代码如下
declare @refdt as datetime
declare @loaddt as datetime
set @refdt='2022-04-01 01:00:00:000'
set @loaddt='2022-04-01 02:00:00:000'
select dt, id, Unit, MIN(pmVoltage) as[test]
from energymeter15min
where (id LIKE '3_4237') and (dt BETWEEN @refdt and @loaddt)
group by dt, Unit, pmVoltage, id
order by Unit, dt
此查询的结果如下所示
但我只是想在@refdt 和@loaddt 之间的时间跨度内显示 pmVoltage 的最小值 像这样
尝试从 GROUP BY 中取出 pmVoltage - 最小值由 group by 确定,不应包含在其中。
group by dt, Unit, id
order by Unit, dt
试一试,如果有效请告诉我。对于具有最低 pmVoltage 值的每个 Unit/id 组合,它应该只显示一条记录。
declare @refdt as datetime;
declare @loaddt as datetime;
set @refdt='2022-04-01 01:00:00:000';
set @loaddt='2022-04-01 02:00:00:000';
With data AS(
select dt, id, Unit, pmVoltage test
, ROW_NUMBER() OVER(Partition BY Unit, id ORDER BY pmVoltage) rowNum
from energymeter15min
where id LIKE '3_4237'
and dt BETWEEN @refdt and @loaddt
)
select dt, id, Unit, test
from data
where rowNum = 1
我选择了完全不同的设置
declare @refdt as datetime ='2022-04-06 01:00:00:000'
declare @loaddt as datetime ='2022-04-06 04:00:00:000'
declare @id as int =330647
select min(dt) as startdt, max(dt) as stopdt,
energymeter15min.id,
Unit,
min(pmVoltage) as minVoltage,
max(pmVoltage) as maxVoltage,
min(pmMinPowerConsumption)as minPowerConsumption,
max(pmMaxPowerConsumption) as maxPowerConsumption
from energymeter15min
join fruname on energymeter15min.frunameid = fruname.id
where (dt between @refdt and @loaddt )
and right(energymeter15min.id,4) in ( right(@id,4) )
and Unit <>'BB-1'
group by energymeter15min.id,Unit
order by Unit asc
结果是否呈现为