SQLite:获取所有条目的每月、每周或每日平均值
我有一个SQLite记录数据库,其格式如下:SQLite:获取所有条目的每月、每周或每日平均值,sql,sqlite,Sql,Sqlite,我有一个SQLite记录数据库,其格式如下: date location temperature 1568463916 room 1 20.0 1568463916 room 2 25.0 1568463916 room 3 30.0 ... 1568460316 room 1 15.5 1568460316 room 2 20.5 1568460316 room 3 21.3 每小时插入三条新记录,每个房间一条 对于月平均值,需
date location temperature
1568463916 room 1 20.0
1568463916 room 2 25.0
1568463916 room 3 30.0
...
1568460316 room 1 15.5
1568460316 room 2 20.5
1568460316 room 3 21.3
每小时插入三条新记录,每个房间一条
对于月平均值,需要此输出:
month avg_temperature location
01 21.333 room 1
01 24.5 room 2
01 19.0 room 3
...
12 20.4 room 1
12 31.31 room 2
12 13.37 room 3
相同的查询可能会被重用,以获得每周平均值(第00-07天)和每日平均值(第00-23小时)
要获得月平均值,我假设我将选择:
- 所有日期在
和now
now-1年之间的记录
asstrftime(日期,“unixepoch”)
Month
- 对于每个
,然后对于每个位置
获取月
平均温度
- 结果是
房间*12排
每个房间每月的平均温度
groupby
语句时,我只得到每个月的最后一行。构造这种查询的正确方法是什么
这是我尝试过的查询:
SELECT strftime("%m", date, "unixepoch") month,
avg(temperature) avg_temperature,
location
FROM table
WHERE date > date("now", "unixepoch", "-1 year")
AND date < date("now", "unixepoch")
GROUP BY location, month
ORDER BY month
选择strftime(“%m”,日期,“unixepoch”)月,
平均(温度)平均温度,
位置
从桌子上
其中日期>日期(“现在”,“unixepoch”,“1年”)
日期<日期(“现在”,“unixepoch”)
按地点、月份分组
按月订购
这应该满足您的要求:
select date(datetime(date, 'unixepoch'), 'start of month') as month,
location,
avg(temperature)
from t
group by date(datetime(date, 'unixepoch'), 'start of month') as month,
location
order by month, location;
请发布您尝试过的内容?@AnkitBajpai我已经更新了我的帖子。我已经用我最初尝试过的查询更新了我的帖子。我是否可以使用
按月份、地点分组,而不是按日期分组(…)作为月份、地点分组?@www。对SQLite在组中支持别名。我想(datetime(date,'unixepoch'),'start of month')
的作用与strftime('%m',date)
相同?我需要为我拥有的每个位置选择过去一年每个月内记录的平均值,因此第01个月,平均值20.0,位置房间1
将意味着在1月份,房间1的平均温度为20.0,但是数据库中存储的值每小时更新一次。@www。不,我认为他们不一样。