1

我正在使用 Microsft SQL 2008。我需要找到前 30 天运行的单个 Daily Max 值。这是我目前正在使用的查询和我目前得到的示例结果。由于数据只会随着时间的推移而增加,我真的只需要当天的最后一个条目。我知道有不同的方法可以得到我正在寻找的东西我只需要一些帮助才能找到正确的方向。

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
4

1 回答 1

2

您应该能够使用 SQL Server排名功能,例如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;

请参阅带有演示的 SQL Fiddle

如果您想一次返回多个,则可以将其扩展到partition数据,因此查询将是:namedatename

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;
于 2013-03-26T17:34:52.027 回答