Sql 在接下来的几个小时内,从两个日期差中找出一列的最小值
找到从未来72小时到2020-07-06T00:00:00.000Z日期的最低温度值的可能方法Sql 在接下来的几个小时内,从两个日期差中找出一列的最小值,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,找到从未来72小时到2020-07-06T00:00:00.000Z日期的最低温度值的可能方法 SELECT [WeatherForcastDetails].[StartTime], [WeatherForcastDetails].[Temperature], [WeatherForcastDetails].[Id] FROM [WeatherForcastDetails]WHERE ([WeatherForcastDetails].[StartTime] > N'2020-07-06T
SELECT [WeatherForcastDetails].[StartTime],
[WeatherForcastDetails].[Temperature],
[WeatherForcastDetails].[Id]
FROM [WeatherForcastDetails]WHERE ([WeatherForcastDetails].[StartTime] > N'2020-07-06T00:00:00.000Z'
AND [WeatherForcastDetails].[StartTime] < N'2020-07-10T00:00:00.000Z')
AND [WeatherForcastDetails].[WeatherForcastId] in (6146)
另外如果我有更多的天气预报,比如
SELECT wfd.StartTime,wfd.Temperature,wfd.WeatherForcastId
FROM WeatherForcastDetails as wfd
WHERE (wfd.StartTime> N'2020-0706T00:00:00.000Z'
AND wfd.StartTime < N'2020-07-10T00:00:00.000Z')
AND wfd.WeatherForcastId in (6146,6163,6180)
我需要在查询检查三天温度(最小值)中找到给定日期范围内未来72小时内温度最低的每天数据。根据WeatherForcastId,WeatherForcastId可以重复
暗示
select case when val1 < val2 then
case when val1 < val3 then val1
else val3
end
when val2 < val3 then val2
else val3
end
来自科尔斯
或者类似的
SELECT
MIN([WeatherForcastDetails].[Temperature]),
MIN([WeatherForcastDetails].[WeatherForcastId]),
(
SELECT MIN(wfd.Temperature)
FROM WeatherForcastDetails as wfd WHERE (wfd.StartTime> N'2020-07-06T00:00:00.000Z'
AND wfd.StartTime < DATEADD(hour, 72, N'2020-07-06T00:00:00.000Z'))) as minTpr
FROM [WeatherForcastDetails]
WHERE ([WeatherForcastDetails].[StartTime] > N'2020-07-06T00:00:00.000Z'
AND [WeatherForcastDetails].[StartTime] < N'2020-07-10T00:00:00.000Z')
AND [WeatherForcastDetails].[WeatherForcastId] in
(6146,6163,6180,6198,6238,6244,6250)
GROUP BY [WeatherForcastDetails].[StartTime]
如果我没有听错,一个选项是过滤,对结果进行排序并仅保留第一行:
select top(1) w.*
from WeatherForcastDetails w
where
WeatherForcastId = 6146
and StartTime >= '20200706'
and StartTime < '20200710'
order by Temperature
我想你想要:
SELECT TOP (1) wfd.*
FROM WeatherForcastDetails wfd
WHERE wfd.StartTime >= '2020-07-06' AND
wfd.StartTime < DATEADD(hour, 72, '2020-07-06')
wfd.WeatherForcastId in (6146)
ORDER BY wfd.Temperature ASC;
从当前时间算起是72小时吗?不是从当前时间或GETDATE算起,预期结果是64。您所说的预期结果是64是什么意思?选择DATEDIFFhour,N'2020-07-06T00:00:00.000Z',N'2020-07-08T13:00:00.000Z'作为DateDiff;结果是61小时,小于72小时,因此从开始日期起接下来的72小时,得到的温度=64。比较从开始日期起到接下来的72小时的温度,找出温度的最低值。抱歉,我正在寻找一般解决方案。希望将MinStartDate与下一个72小时的温度进行比较,并找到温度的最低值如果我选择37小时差,则选择DATEDIFFhour,N'2020-07-06T00:00:00.000Z',N'2020-07-07 13:00:00.000'作为DateDiff;那么最低温度将是65℃,这实际上并不能解决我的问题。我已经扩展了我的问题,请看一看。@ABDULJAMAL。您的编辑完全更改了问题,应该作为新问题提问。你实际提出的问题被回答了多次。在本例中,我认为您是一个比实际更新的用户,因此编辑了您修改后问题的答案。
SELECT wfd.*
FROM (SELECT wfd.*,
ROW_NUMBER() OVER (PARTITION BY wfd.WeatherForcastId ORDER BY wfd.Temperature ASC) as seqnum
FROM WeatherForcastDetails wfd
WHERE wfd.StartTime >= '2020-07-06' AND
wfd.StartTime < DATEADD(hour, 72, '2020-07-06')
wfd.WeatherForcastId in (6146)
) wfd
WHERE seqnum = 1;