Here's my query:
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
Basically I have some weather data that I group into hour intervals (offset by 30 minutes) and I want to specifically count the number of times I get a particular weather description (and matching icon) in that time interval and select the weather description within that time interval with the highest occurrence (count) (desc_table). Then I want to get the average temperatures within that time segment ((avg_temp_table)(maybe I need a sub-query? To do this avg instead of how I have it) and join the two queries along their hour columns.
I want my anchor to be based off of what hour the query is made (now) and count the occurrences and then the next members will subtract an hour each time and go to the next time interval and count, etc.
Sample data, there will be a lot more rows within each time segment for a regular dataset {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"
Output is a join between the avg temperatures of the time interval (avg_temp_table) with the output of the first aggregate 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"
Right now I'm getting a no such column error because my anchor comes from my weather_events table and so do my recursive members. I get the "recursive aggregate queries not supported error" when I change the recursive members from to desc_table. But I don't want to get my recursive members from the desc_table, I want to segment by hour and then go through each hour interval and get the count. I'm guessing I'm doing the anchor incorrectly to begin with also.