简体   繁体   English

获取每天的最小值和最大值之间的差异

[英]Get difference between Min and Max value of each day

I store sensor readings in a table, the data is meter readings:我将传感器读数存储在表中,数据是仪表读数:

CaptureDate               SensorID      Value
2020-01-11 14:15:33.350   121           23908,0000
2020-01-11 14:00:33.300   123           23161,0000
2020-01-11 14:00:33.240   121           23901,0000
2020-01-11 13:45:33.137   123           23154,0000
2020-01-11 13:45:33.073   121           23894,0000
2020-01-11 13:30:32.927   123           23147,0000

I need to use SQL to get the daily totals for a month filtered by SensorID, to get something similar to this:我需要使用 SQL 来获取按 SensorID 过滤的一个月的每日总数,以获得类似于以下内容的内容:

Date        SensorID    Value
2020-01-10  121         319
2020-01-11  121         249
2020-01-12  121         289
2020-01-13  121         263
2020-01-14  121         314
2020-01-15  121         248

I have tried to obtain minimum and maximum values grouped by day but I can't get the difference of the counter to obtain the net value;我试图获得按天分组的最小值和最大值,但我无法通过计数器的差异来获得净值;

SELECT * 
FROM Records
WHERE CaptureDate in 
(
    SELECT min(CaptureDate)
    FROM Records
    WHERE SensorID = 124
        AND convert(date, CaptureDate) >= '2020-01-01'
    GROUP BY convert(date, CaptureDate)
) OR CaptureDate IN (
    SELECT Max(CaptureDate)
    FROM Records
    WHERE SensorID = 124
        AND convert(date, CaptureDate) >= '2020-01-01'
    GROUP BY convert(date, CaptureDate)
) ORDER BY CaptureDate

And return:并返回:

CaptureDate               SensorID  Value   
2020-01-08 14:20:39.627   121       23601.0000
2020-01-08 17:50:39.843   121       23678.0000
2020-01-09 08:50:19.473   121       23678.0000
2020-01-09 18:05:20.300   121       23707.0000
2020-01-10 08:46:06.903   121       23707.0000
2020-01-10 18:15:20.007   121       23796.0000

Try that试试看

With DailyMinMax AS
(
  SELECT 
    CAST(CaptureDate AS date) date,
    SensorID,
    MIN(Value) minvalue,
    Max(Value) maxvalue
  FROM Records 
  GROUP BY CAST(CaptureDate AS date), SensorID
)
SELECT 
  date,
  SensorID,
  maxvalue-minvalue AS MaxMinDifference 
FROM DailyMinMax 

I have not tested it, but the solution should be something similar to this.我没有测试过,但解决方案应该与此类似。 you create two different tables, one with the daily minimum and one with the dailt maximum and then join them:你创建了两个不同的表,一个是每日最小值,一个是每日最大值,然后加入它们:

SELECT mx.CaptureDate, mx.value - mn.value FROM
(
    SELECT CaptureDate, min(CaptureDate) value
    FROM Records
    WHERE SensorID = 124
        AND convert(date, CaptureDate) >= '2020-01-01'
    GROUP BY convert(date, CaptureDate)
) mn, (
    SELECT CaptureDate, Max(CaptureDate) value
    FROM Records
    WHERE SensorID = 124
        AND convert(date, CaptureDate) >= '2020-01-01'
    GROUP BY convert(date, CaptureDate)
) mx 
WHERE mx.CaptureDate = mn.CaptureDate
ORDER BY mx.CaptureDate  

I found the solution!我找到了解决方案! There is nothing better than exposing the problem to clarify my ideas没有什么比暴露问题来澄清我的想法更好的了

SELECT SensorID, CAST(CaptureDate AS DATE) AS Date, MAX(Value)-MIN(Value) As dValue
FROM Readings
WHERE SensorID = 121
GROUP BY CAST(CaptureDate AS DATE), SensorID
ORDER BY CaptureDate

How easy it is now!现在是多么容易啊! :D :D

This should work:这应该有效:

Demo: DB Fiddle演示: DB Fiddle

select 
  convert(date, capturedate) date_only, 
  sensorid, 
  min(value) min_val, 
  max(value) max_val,
  max(value) - min(value) diff
from records
group by convert(date, capturedate), sensorid

I suggest try and store aggregated count in a separate column and run your query on that column.我建议尝试将聚合计数存储在单独的列中,然后在该列上运行查询。 I don't think group by aggregates persist and passed to outer query.我不认为按聚合分组持久化并传递给外部查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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