我正在使用Microsft SQL 2008。 我需要找到前30天运行的每日最大值。这是我目前正在使用的查询以及我目前获得的示例结果。由于数据仅随着日期的增加而递增,因此我真的只需要当天的最终输入。我知道有不同的方法来获得我正在寻找的东西我只需要一些帮助就可以指明正确的方向。
SELECT HISTORY.STATION, HISTORY.NAME, HISTORY.VALUE, HISTORY.TIME
FROM DATA.dbo.HISTORY HISTORY
WHERE (HISTORY.NAME='KWMAXD') AND (HISTORY.TIME>= dateadd("Day",-30,getdate()))
ORDER BY HISTORY.TIME, HISTORY.NAME DESC
SYST KWMAXD 49040 2013-02-24 12:59:00.000
SYST KWMAXD 49040 2013-02-24 13:59:00.000
SYST KWMAXD 49040 2013-02-24 14:59:00.000
SYST KWMAXD 49040 2013-02-24 15:59:00.000
SYST KWMAXD 49040 2013-02-24 16:59:00.000
SYST KWMAXD 49040 2013-02-24 17:59:00.000
SYST KWMAXD 49040 2013-02-24 18:59:00.000
SYST KWMAXD 49040 2013-02-24 19:59:00.000
SYST KWMAXD 49040 2013-02-24 20:59:00.000
SYST KWMAXD 49040 2013-02-24 21:59:00.000
SYST KWMAXD 49040 2013-02-24 22:59:00.000
SYST KWMAXD 49040 2013-02-24 23:59:00.000
SYST KWMAXD 28160 2013-02-25 00:59:00.000
SYST KWMAXD 28160 2013-02-25 01:59:00.000
SYST KWMAXD 28160 2013-02-25 02:59:00.000
SYST KWMAXD 28160 2013-02-25 03:59:00.000
SYST KWMAXD 28160 2013-02-25 04:59:00.000
SYST KWMAXD 30110 2013-02-25 05:59:00.000
SYST KWMAXD 33390 2013-02-25 06:59:00.000
SYST KWMAXD 33390 2013-02-25 07:59:00.000
SYST KWMAXD 33390 2013-02-25 08:59:00.000
SYST KWMAXD 33390 2013-02-25 09:59:00.000
SYST KWMAXD 33390 2013-02-25 10:59:00.000
SYST KWMAXD 33390 2013-02-25 11:59:00.000
SYST KWMAXD 33390 2013-02-25 12:59:00.000
SYST KWMAXD 33390 2013-02-25 13:59:00.000
SYST KWMAXD 33390 2013-02-25 14:59:00.000
SYST KWMAXD 33390 2013-02-25 15:59:00.000
SYST KWMAXD 36100 2013-02-25 16:59:00.000
SYST KWMAXD 40650 2013-02-25 17:59:00.000
SYST KWMAXD 47040 2013-02-25 18:59:00.000
SYST KWMAXD 49650 2013-02-25 19:59:00.000
SYST KWMAXD 49650 2013-02-25 20:59:00.000
SYST KWMAXD 49650 2013-02-25 21:59:00.000
SYST KWMAXD 49650 2013-02-25 22:59:00.000
SYST KWMAXD 49650 2013-02-25 23:59:00.000
SYST KWMAXD 43090 2013-02-26 00:59:00.000
SYST KWMAXD 43090 2013-02-26 01:59:00.000
SYST KWMAXD 43090 2013-02-26 02:59:00.000
SYST KWMAXD 43600 2013-02-26 03:59:00.000
SYST KWMAXD 45900 2013-02-26 04:59:00.000
SYST KWMAXD 50900 2013-02-26 05:59:00.000
SYST KWMAXD 55290 2013-02-26 06:59:00.000
SYST KWMAXD 55290 2013-02-26 07:59:00.000
SYST KWMAXD 55290 2013-02-26 08:59:00.000
SYST KWMAXD 55290 2013-02-26 09:59:00.000
SYST KWMAXD 55290 2013-02-26 10:59:00.000
SYST KWMAXD 55290 2013-02-26 11:59:00.000
答案 0 :(得分:2)
您应该能够使用SQL Server ranking functions,例如row_number()
:
select station,
name,
value,
time
from
(
SELECT h.STATION, h.NAME, h.VALUE, h.TIME,
row_number() over(partition by cast(h.time as date) order by h.time desc) rn
FROM HISTORY h
WHERE (h.NAME='KWMAXD')
AND (h.TIME>= dateadd(DAY,-30,cast(getdate() as date)))
) src
where rn = 1;
如果您希望一次返回多个partition
,可以将name
和date
扩展为name
数据,因此查询将是:
select station,
name,
value,
time
from
(
SELECT h.STATION, h.NAME, h.VALUE, h.TIME,
row_number() over(partition by h.name, cast(h.time as date) order by h.time desc) rn
FROM HISTORY h
WHERE (h.TIME>= dateadd(DAY,-30,cast(getdate() as date)))
) src
where rn = 1;