Hi I’m using the MySQL query below along side Grafana, and data relating to my electricity usage. The data collected ranges from late 2021 to current date, however the result only ranges from December 21 to November 22.
SELECT
Time AS "time",
SUM(Rate/800) as kWh
FROM Log
GROUP BY MONTH(STR_TO_DATE(time, "%Y-%m-%d"))
Can someone kindly assist with amending the query to display all relevant months from 2021 to 2023 and so on please.
3
Answers
Try this :
You can use this query to get your desired output :
Results looks like :
Please add your table structure and some sample data to your question.
Your current query is non-deterministic, and if you had ONLY_FULL_GROUP_BY enabled, would be failing with a 1055 error. You can read more about MySQL Handling of GROUP BY here.
You are grouping by
MONTH(STR_TO_DATE(time, "%Y-%m-%d"))
which will return 1 for2021-01-02
,2022-01-03
and2023-01-04
. You need to group by both the year and month.If you run this modified version of your query you should be able to see what is happening:
Your use of
STR_TO_DATE()
appears unnecessary as you are using the standard date format%Y-%m-%d
.With the date format
'%Y-%m'
,2021-01-02
,2022-01-03
and2023-01-04
will be returned as2021-01
,2022-01
and2023-01
respectively, and grouped in the correct month.