Table structure – Data present for 5 min. slots –
data_point | point_date
12 | 00:00
14 | 00:05
23 | 00:10
10 | 00:15
43 | 00:25
10 | 00:40
When I run the query for say 30 mins. and if data is present I’ll get 6 rows (one row for each 5 min. stamp). Simple Query –
select data_point
from some_table
where point_date >= start_date
AND point_date < end_date
order by point_date
Now when I don’t have an entry for a particular time slot (e.g. time slot 00:20 is missing), I want the “data_point” to be returned as 0
The REPLACE, IF, IFNULL, ISNULL don’t work when there no rows returned.
I thought Union with a default value would work, but it failed too or maybe I didn’t use it correctly.
Is there a way to get this done via sql only ?
Note : Python 2.6 & mysql version 5.1
Yes, you can do that using SQL only. A solution would be to use a Stored Routine. The bellow Stored Procedure produces following output:
The table I used:
Here the Stored Procedure (adjust for your environment):
If you save the above into a file called ‘per5minproc.sql’, you can load it like this:
In Python using MySQLdb (I didn’t get this working in MySQL Connector/Python, me ashamed!):
The solution above works, but probably will need some tweaking, e.g. dtStart can be an argument to the SP.
And, it’s indeed all SQL!