这是我的查询:
WITH desc_table(counter, hourly, current_weather_description, current_icons, time_stamp) AS (
Select count(*) AS counter, CASE WHEN strftime('%M', 'now') < '30'
THEN strftime('%H', 'now')
ELSE strftime('%H', time_stamp, '+1 hours') END as hourly,
current_weather_description,
current_icons,
time_stamp
From weather_events
GROUP BY strftime('%H', time_stamp, '+30 minutes'), current_weather_description
UNION ALL
Select count(*) as counter, hourly - 1, current_weather_description, current_icons, time_stamp
From weather_events
GROUP BY strftime('%H', time_stamp, '+30 minutes'), current_weather_description
Order By counter desc limit 1
),
avg_temp_table(avg_temp, hour_seg, time_stamp) AS (
select avg(current_temperatures) as avg_temp, CASE WHEN strftime('%M', time_stamp) < '30'
THEN strftime('%H', time_stamp)
ELSE strftime('%H', time_stamp, '+1 hours') END as hour_seg,
time_stamp
from weather_events
group by strftime('%H', time_stamp, '+30 minutes')
order by hour_seg desc
)
Select hourly, current_weather_description
from desc_table
join avg_temp_table
on desc_table.hourly=avg_temp_table.hour_seg
基本上我有一些天气数据,我按小时间隔分组(偏移 30 分钟),我想专门计算在该时间间隔内获得特定天气描述(和匹配图标)的次数,并在其中选择天气描述出现次数最多的时间间隔(计数)(desc_table)。然后我想获得该时间段内的平均温度((avg_temp_table)(也许我需要一个子查询?要做到这一点而不是我如何拥有它)并沿着他们的小时列加入两个查询。
我希望我的锚基于查询的时间(现在)并计算出现次数,然后下一个成员将每次减去一个小时并进入下一个时间间隔并计数等。
样本数据,对于常规数据集 {current_temperatures, current_weather_description, current_icons, time_stamp},每个时间段内会有更多的行:
"87" "Rain" "rainicon" "2016-01-20 02:15:08"
"65" "Snow" "snowicon" "2016-01-20 02:39:08"
"49" "Rain" "rainicon" "2016-01-20 03:15:08"
"49" "Rain" "rainicon" "2016-01-20 03:39:08"
"46" "Clear" "clearicon" "2016-01-20 04:15:29"
"46" "Clear" "clearicon" "2016-01-20 04:38:53"
"46" "Cloudy" "cloudyicon" "2016-01-20 05:15:08"
"46" "Clear" "clearicon" "2016-01-20 05:39:08"
"45" "Clear" "clearicon" "2016-01-20 06:14:17"
"45" "Clear" "clearicon" "2016-01-20 06:34:23"
"45" "Clear" "clearicon" "2016-01-20 07:24:54"
"45" "Rain" "rainicon" "2016-01-20 07:44:41"
"43" "Rain" "rainicon" "2016-01-20 08:19:08"
"36" "Clear" "clearicon" "2016-01-20 08:39:08"
"35" "Meatballs" "meatballsicon" "2016-01-20 09:18:08"
"18" "Cloudy" "cloudyicon" "2016-01-20 09:39:08"
输出是时间间隔的平均温度 (avg_temp_table) 与第一个聚合 CTE (desc_table) {avg_temp, weather_description, current_icon} 的输出之间的连接:
"87" "Rain" "rainicon"
"57" "Rain" "rainicon"
"47" "Clear" "clearicon"
"46" "Clear" "clearicon"
"46" "Cloudy" "cloudyicon"
"45" "Clear" "clearicon"
"44" "Rain" "rainicon"
"36" "Clear" "clearicon"
"18" "Cloudy" "cloudyicon"
现在我没有这样的列错误,因为我的锚来自我的 weather_events 表,我的递归成员也是如此。当我将递归成员从更改为 desc_table 时,出现“不支持递归聚合查询错误”。但我不想从 desc_table 中获取我的递归成员,我想按小时分段,然后通过每个小时间隔并获取计数。我猜我一开始也做错了锚。