我有一个sql表,每15分钟存储一次数据,但是我想每4小时获取一次最大值。
这是我的实际表:
+----+----+----+-------------------------+
| Id | F1 | F2 | timestamp |
+----+----+----+-------------------------+
| 1 | 24 | 30 | 2019-03-25 12:15:00.000 |
| 2 | 22 | 3 | 2019-03-25 12:30:00.000 |
| 3 | 2 | 4 | 2019-03-25 12:45:00.000 |
| 4 | 5 | 35 | 2019-03-25 13:00:00.000 |
| 5 | 18 | 23 | 2019-03-25 13:15:00.000 |
| ' | ' | ' | ' |
| 16 | 21 | 34 | 2019-03-25 16:00:00.000 |
+----+----+----+-------------------------+
我正在寻找的输出是:
+----+----+----+
| Id | F1 | F2 |
+----+----+----+
| 1 | 24 | 35 |1st 4 Hours
+----+----+----+
| 2 | 35 | 25 |Next 4 Hours
+----+----+----+
我确实使用了查询
select max(F1) as F1,
max(F2) as F2
from table
where timestamp>='2019/3/26 12:00:01'
and timestamp<='2019/3/26 16:00:01'
它返回前4小时的值,但是当我将时间戳从4小时增加到8小时时,它仍然会给我1个最大值,而不是每4小时2个。
我确实尝试了group by子句,但无法获得预期的结果。
答案 0 :(得分:1)
这是一个相对简单的方法:
select convert(date, timestamp) as dte,
(datepart(hour, timestamp) / 4) * 4 as hour,
max(F1) as F1,
max(F2) as F2
from table
group by convert(date, timestamp), (datepart(hour, timestamp) / 4) * 4;
这将日期和小时放在单独的列中;您可以使用dateadd()
将它们放在一列中。
答案 1 :(得分:1)
这应该有效
from aiohttp import ProxyConnector, BasicAuth
basic_auth = BasicAuth(USER_PROXY_LOGIN, USER_PROXY_PASS)
connector = ProxyConnector(USER_PROXY, proxy_auth=basic_auth)
cient = discord.Client(connector=connector)
答案 2 :(得分:0)
尝试此查询:
declare @startingDatetime datetime = '2017-10-04 12:00:00';
select grp, max(F1) F1, max(F2) F2
from (
select datediff(hour, @startingDatetime, [timestamp]) / 4 grp, *
from MyTable
where [timestamp] > @startingDatetime
) a group by grp