我有一个名为Thermo的表,每30分钟存储两个不同的温度
(dt =时间| Ti =内部温度| To =外部温度)
我想交替获取白天和黑夜的平均值。 这可以通过分组小时 06:00-17:59 和 18:00-05:59
我能做的最好的就是按照以下代码对00:00-11:59和12:00-23:59进行分组:
SELECT CAST(strftime('%m%d', dt) AS TIME) || CAST(strftime('%H', dt)/12 AS TIME) AS time,
round(avg(Ti), 1) AS Ti,
round(avg(To), 1) AS To,
FROM thermo WHERE dt > datetime(CURRENT_TIMESTAMP, 'localtime', '-10 days')
GROUP BY time ORDER BY time;
是否可以改变时间组?
答案 0 :(得分:2)
您可以使用CASE
来确定感兴趣的时间段:
SELECT CAST(strftime('%m%d', dt) AS TIME) as dt,
(CASE WHEN strftime('%H', dt) BETWEEN 6 AND 17 THEN 'daytime'
ELSE 'nightime'
END) as period,
round(avg(Ti), 1) as Ti,
round(avg(To), 1) as To
FROM thermo
WHERE dt > datetime(CURRENT_TIMESTAMP, 'localtime', '-10 days')
GROUP BY dt, period
ORDER BY time;