SQL查询按月份部分时间戳进行分组 [英] SQL query to group by month part of timestamp
问题描述
这是我目前的查询:
SELECT TIMESTAMP,SUM(电)AS电,`siteID`
FROM表
WHERE(
MONTH(`TimeStamp `)= 10)
GROUP BY siteID
我的表格如下所示:
############################## ###########
#Id#SiteID#TimeStamp#Elecricity#
#0#100#10/08/2012#50#
#1#98# 10/08/2012#32#
#2#100#10/09/2012#96#
#3#94#2012年10月9日#25#
#4#100 #10/10/2012#100#
#5#100#10/11/2012#55#
#################### #####################
我试图做的是总结每个网站的每个月的所有日子,以便每个网站和每个月都有一个答案。因此,在这个例子中,查询应该返回siteID 100的电量值的总和,因为它们全部在第10个月,而siteID 98和94的电量值相同。
谢谢
选择月份('TIMESTAMP'),SUM(电) siteID`
FROM table
WHERE(
MONTH(`TimeStamp`)= 10)
GROUP BY siteID,month('TIMESTAMP')
这会起作用。你必须考虑的一件事是那个月不是独一无二的。 2012年10月,在这种情况下,与2013年10月相同。您可能需要为年份添加另一列。
I'm really bad at SQL queries but I'm learning so please forgive this question.
Here is my current query:
SELECT TIMESTAMP, SUM( electricity ) AS electricity, `siteID`
FROM table
WHERE (
MONTH( `TimeStamp` ) =10)
GROUP BY siteID
My table looks like:
#########################################
# 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 am trying to do is to sum up all of the days of each month of each site, so that I will have one answer per site and month. So in this example the query should return the sum of the electricity values for siteID 100 because they're all in month 10, the same for siteID 98 and 94.
Thanks
SELECT month('TIMESTAMP'), SUM( electricity ) AS electricity, `siteID`
FROM table
WHERE (
MONTH( `TimeStamp` ) =10)
GROUP BY siteID, month('TIMESTAMP')
This will work. One thing that you have to think about is that month is not unique. Oct, 2012, in this case, is the same as Oct 2013. You might want to add another column for year.
这篇关于SQL查询按月份部分时间戳进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!