Post by impactoz on Aug 19, 2019 1:19:04 GMT -8
So while experimenting and making my weather station tweet daily information... I needed to develop some SQL Queries to get some daily data....
Thought it might be of interest to someone, or at least its an archive in case I loose my notes!
You can run these if you have the database logging turned on, and simplest way is through phpmyadmin….
I think I have them right - still testing and playing... The biggest issue is trying to get daily data for the day when you are running it - and all the data is stored in UTC time format... which means an absolute huge hassle, and extra cpu usage - arghh… So wherever you see +10:00 on the sql queries - change it to your own offset for your local timezone.
Easily modified for daily, weekly, monthly - but because I am just tweeting daily, I want to run this at end of day and just get data for that day....
Todays Total Rainfall
SELECT MAX(`totalRain`) FROM `WeatherData` WHERE Date(CONVERT_TZ(TimeStamp,'+00:00','+10:00')) = Date(NOW())
Todays Max Temperature
SELECT `outsideTemperature`, TIME_FORMAT(Time(CONVERT_TZ(TimeStamp,'+00:00','+10:00')),'%h:%i %p') As LocalTm FROM `WeatherData` WHERE `outsideTemperature` in (SELECT Max(`outsideTemperature`) FROM `WeatherData` WHERE Date(CONVERT_TZ(TimeStamp,'+00:00','+10:00')) = Date(NOW())) LIMIT 1
Todays Min Temperature
SELECT `outsideTemperature`, TIME_FORMAT(Time(CONVERT_TZ(TimeStamp,'+00:00','+10:00')),'%h:%i %p') As LocalTm FROM `WeatherData` WHERE `outsideTemperature` in (SELECT Min(`outsideTemperature`) FROM `WeatherData` WHERE Date(CONVERT_TZ(TimeStamp,'+00:00','+10:00')) = Date(NOW())) LIMIT 1
Largest Wind Gust Today
SELECT MAX(`currentWindGust`), CONVERT_TZ(TimeStamp, '+00:00', @@session.time_zone) FROM `WeatherData` WHERE `TimeStamp` > DATE_SUB(UTC_TIMESTAMP(), INTERVAL 1 DAY)
Lightning Count for the Day
SELECT `as3935LightningCount` FROM `WeatherData` WHERE `as3935LightningCount` in (SELECT Max(`as3935LightningCount`) FROM `WeatherData` WHERE Date(CONVERT_TZ(TimeStamp,'+00:00','+10:00')) = Date(NOW())) LIMIT 1
Furthest Lightning
SELECT MAX(`as3935LastDistance`) FROM `WeatherData` WHERE Date(CONVERT_TZ(TimeStamp,'+00:00','+10:00')) = Date(NOW()) AND `as3935LastDistance`> 0 LIMIT 1
Closest Lightning
SELECT MIN(`as3935LastDistance`) FROM `WeatherData` WHERE Date(CONVERT_TZ(TimeStamp,'+00:00','+10:00')) = Date(NOW()) AND `as3935LastDistance`> 0 LIMIT 1
Last Date Lightning detected
SELECT DATE_FORMAT(CONVERT_TZ(Max(TimeStamp),'+00:00','+10:00'),'%a %d %b %Y'), TIME_FORMAT(Time(CONVERT_TZ(Max(TimeStamp),'+00:00','+10:00')),'%h:%i %p') FROM `WeatherData` where `as3935LightningCount` > 0 LIMIT 1
UV Index Max and When for today
SELECT `UVIndex`, TIME_FORMAT(Time(CONVERT_TZ(TimeStamp,'+00:00','+10:00')),'%h:%i %p') As LocalTm FROM `Sunlight` WHERE `UVIndex` in (SELECT Max(`UVIndex`) FROM `Sunlight` WHERE Date(CONVERT_TZ(TimeStamp,'+00:00','+10:00')) = Date(NOW())) LIMIT 1
Daily Avg UV Index - but just during the day
SELECT Avg(`UVIndex`) FROM `Sunlight` WHERE Date(CONVERT_TZ(TimeStamp,'+00:00','+10:00')) = Date(NOW()) And `UVIndex` > 0
Hope it helps someone....
Thought it might be of interest to someone, or at least its an archive in case I loose my notes!
You can run these if you have the database logging turned on, and simplest way is through phpmyadmin….
I think I have them right - still testing and playing... The biggest issue is trying to get daily data for the day when you are running it - and all the data is stored in UTC time format... which means an absolute huge hassle, and extra cpu usage - arghh… So wherever you see +10:00 on the sql queries - change it to your own offset for your local timezone.
Easily modified for daily, weekly, monthly - but because I am just tweeting daily, I want to run this at end of day and just get data for that day....
Todays Total Rainfall
SELECT MAX(`totalRain`) FROM `WeatherData` WHERE Date(CONVERT_TZ(TimeStamp,'+00:00','+10:00')) = Date(NOW())
Todays Max Temperature
SELECT `outsideTemperature`, TIME_FORMAT(Time(CONVERT_TZ(TimeStamp,'+00:00','+10:00')),'%h:%i %p') As LocalTm FROM `WeatherData` WHERE `outsideTemperature` in (SELECT Max(`outsideTemperature`) FROM `WeatherData` WHERE Date(CONVERT_TZ(TimeStamp,'+00:00','+10:00')) = Date(NOW())) LIMIT 1
Todays Min Temperature
SELECT `outsideTemperature`, TIME_FORMAT(Time(CONVERT_TZ(TimeStamp,'+00:00','+10:00')),'%h:%i %p') As LocalTm FROM `WeatherData` WHERE `outsideTemperature` in (SELECT Min(`outsideTemperature`) FROM `WeatherData` WHERE Date(CONVERT_TZ(TimeStamp,'+00:00','+10:00')) = Date(NOW())) LIMIT 1
Largest Wind Gust Today
SELECT MAX(`currentWindGust`), CONVERT_TZ(TimeStamp, '+00:00', @@session.time_zone) FROM `WeatherData` WHERE `TimeStamp` > DATE_SUB(UTC_TIMESTAMP(), INTERVAL 1 DAY)
Lightning Count for the Day
SELECT `as3935LightningCount` FROM `WeatherData` WHERE `as3935LightningCount` in (SELECT Max(`as3935LightningCount`) FROM `WeatherData` WHERE Date(CONVERT_TZ(TimeStamp,'+00:00','+10:00')) = Date(NOW())) LIMIT 1
Furthest Lightning
SELECT MAX(`as3935LastDistance`) FROM `WeatherData` WHERE Date(CONVERT_TZ(TimeStamp,'+00:00','+10:00')) = Date(NOW()) AND `as3935LastDistance`> 0 LIMIT 1
Closest Lightning
SELECT MIN(`as3935LastDistance`) FROM `WeatherData` WHERE Date(CONVERT_TZ(TimeStamp,'+00:00','+10:00')) = Date(NOW()) AND `as3935LastDistance`> 0 LIMIT 1
Last Date Lightning detected
SELECT DATE_FORMAT(CONVERT_TZ(Max(TimeStamp),'+00:00','+10:00'),'%a %d %b %Y'), TIME_FORMAT(Time(CONVERT_TZ(Max(TimeStamp),'+00:00','+10:00')),'%h:%i %p') FROM `WeatherData` where `as3935LightningCount` > 0 LIMIT 1
UV Index Max and When for today
SELECT `UVIndex`, TIME_FORMAT(Time(CONVERT_TZ(TimeStamp,'+00:00','+10:00')),'%h:%i %p') As LocalTm FROM `Sunlight` WHERE `UVIndex` in (SELECT Max(`UVIndex`) FROM `Sunlight` WHERE Date(CONVERT_TZ(TimeStamp,'+00:00','+10:00')) = Date(NOW())) LIMIT 1
Daily Avg UV Index - but just during the day
SELECT Avg(`UVIndex`) FROM `Sunlight` WHERE Date(CONVERT_TZ(TimeStamp,'+00:00','+10:00')) = Date(NOW()) And `UVIndex` > 0
Hope it helps someone....