我使用以下示例行的SQLite数据库,列为{id,temperature,description,time_stamp:
1 45 Clear 2016-01-13 10:24:17
2 45 Clear 2016-01-13 10:24:41
3 45 Clear 2016-01-13 10:24:41
4 45 Rain 2016-01-13 10:24:41
5 46 Clear 2016-01-13 10:38:29
6 46 Clear 2016-01-13 10:38:53
7 46 Clear 2016-01-13 10:39:08
8 46 Clear 2016-01-13 10:39:08
我对它们运行此查询,按时间间隔和天气描述对它们进行分组:
SELECT AVG(current_temperatures) AS temp_avg,
CASE WHEN strftime('%M', time_stamp) < '30'
THEN strftime('%H', time_stamp)
ELSE strftime('%H', time_stamp, '+1 hours') END as hour,
current_weather_description,
count(*) as counter
FROM weather_events
GROUP BY strftime('%H', time_stamp, '+30 minutes'), current_weather_description
order by hour desc
使用以下结果{row_num,temp_avg,hour,current_weather_description,counter}:
"46.0" "11" "Clear" "4"
"45.0" "10" "Clear" "3"
"45.0" "10" "Rain" "1"
我的问题是如何通过最大计数器子查询每个小时和组。所以最终我想得到结果:
"46.0" "11" "Clear" "4"
"45.0" "10" "Clear" "3"
我是SQL和SQLite的新手。所有数据都来自同一个表。 此外,由于查询的温度是平均值,它如何选择我要求它选择的其余列?例如,如果您同时选择time_stamp,您将看到它是数据库中的特定time_stamp。是否随机选择要选择的列?
答案 0 :(得分:0)
您的sql查询输出正确。您已按half hour time intervals
和weather description
对行进行了分组。 10:00 time interval
有两种不同的天气:&#34;清除&#34;和&#34; Rain&#34;和10:30 time interval
的一个天气:&#34;清除&#34; 。如果您要从GROUP BY
子句中删除weather_description列,则只能获得两行。
使用common table expressions
的解决方案,仅显示在时间间隔内最常出现的天气描述。
;WITH temp_data AS(
SELECT AVG(current_temperatures) AS temp_avg,
CASE WHEN strftime('%M', time_stamp) < '30'
THEN strftime('%H', time_stamp)
ELSE strftime('%H', time_stamp, '+1 hours') END as hour,
current_weather_description,
count(*) as counter
FROM weather_events
GROUP BY strftime('%H', time_stamp, '+30 minutes'),
current_weather_description
)
SELECT T1.* FROM temp_data AS T1
WHERE T1.counter = (SELECT MAX(counter) FROM temp_data AS T2
WHERE T2.hour = T1.hour)