我正在使用SQL Server。我从各种表格中获取信息,我能够做到这一点。
在我的表格中,我有sampletime
列,每30分钟一次,以及一个值列。我想写一个查询来获得每天的差异(最大 - 最小),我有什么建议吗?
例如:
SIGNAL NAME SAMPLETIME VALUE
Spazio-Percorso 2014-03-06 15:01:50.3170000 219345
Spazio-Percorso 2014-03-06 15:31:50.3260000 219384
Spazio-Percorso 2014-03-06 16:01:50.3380000 219388
Spazio-Percorso 2014-03-06 16:31:50.3460000 219388
Spazio-Percorso 2014-03-06 17:01:50.3590000 219400
Spazio-Percorso 2014-03-06 17:31:50.3690000 219434
Spazio-Percorso 2014-03-06 18:01:50.3760000 219471
在这个例子中,所有行都来自同一天,我想要的是每天只有1行,而值列中想要有当天的MAX-MIN
答案 0 :(得分:2)
试试这个
SELECT [SIGNAL NAME], CAST(SAMPLETIME AS DATE) AS SAMPLEDATE, MAX(VALUE)-MIN(VALUE) As Diff_Value
FROM Table1
GROUP BY CAST(SAMPLETIME AS DATE),[SIGNAL NAME]
答案 1 :(得分:0)
如果您使用的是SQL Server 2008或更高版本,则可以将日期时间列转换为DATE
以消除时间值:
SELECT
[SIGNAL NAME],
CAST(SAMPLETIME AS DATE) AS SAMPLEDATE,
MAX(VALUE) - MIN(VALUE) AS VALUE
FROM Table1
GROUP BY [SIGNAL NAME], CAST(SAMPLETIME AS DATE)