SQL query to group by month timestamp

I am very poor at SQL queries, but I am learning, so please forgive this question.

Here is my current request:

SELECT TIMESTAMP, SUM( electricity ) AS electricity, `siteID` FROM table WHERE ( MONTH( `TimeStamp` ) =10) GROUP BY siteID 

My table looks like this:

 ######################################### # Id # SiteID # TimeStamp # Elecricity # # 0 # 100 # 10/08/2012 # 50 # # 1 # 98 # 10/08/2012 # 32 # # 2 # 100 # 10/09/2012 # 96 # # 3 # 94 # 10/09/2012 # 25 # # 4 # 100 # 10/10/2012 # 100 # # 5 # 100 # 10/11/2012 # 55 # ######################################### 

What I'm trying to do is summarize all the days of each month of each site so that I have one answer to the site and a month. Thus, in this example, the query should return the sum of the electricity values ​​for siteID 100, because they are all in month 10, the same for siteID 98 and 94.

thanks

+4
source share
1 answer
 SELECT month('TIMESTAMP'), SUM( electricity ) AS electricity, `siteID` FROM table WHERE ( MONTH( `TimeStamp` ) =10) GROUP BY siteID, month('TIMESTAMP') 

It will work. One thing you should think about is that the month is not unique. Oct, 2012, in this case, is the same as October 2013. You might want to add another column in a year.

+10
source

Source: https://habr.com/ru/post/1495588/


All Articles