个别日子的总和

时间:2014-03-10 13:44:51

标签: mysql sql

在这里,我已经取消了2007年第一天的天然气消耗量。时间以UTC为单位,因此当天从早上5点开始运行24小时。

select sum(b.kwh) as consumption
from BATimeSeries b
where 
    b.companyNo = 1 and 
    b.DayTime >= '2007-01-01 05:00:00.000' and 
    b.DayTime < '2007-01-02 05:00:00.000'

这里它只给出了一个单行(消耗)的表格,它给出了当天的耗电量(以kWh为单位)。但我真的希望有一个跨越一段时间的表,例如一周,一个月或一年。每行有两列:day,consumption,列出区间中的每一天以及该特定日期的消耗总和。我无法想出会在几天内汇总,然后在这几天中每天汇总一天。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:2)

一个简单的GROUP BY就足够了;

SELECT DAY(daytime - INTERVAL 5 HOUR) day, SUM(b.kwh) consumption
FROM BATimeSeries b
WHERE b.companyNo = 1
GROUP BY day;

An SQLfiddle to test with

答案 1 :(得分:1)

这样的东西?

SELECT DATE(b.DayTime),sum(b.kwh) as consumption
FROM BATimeSeries b
WHERE
    b.companyNo = 1 and 
    b.DayTime >= '2007-01-01 05:00:00.000' and 
    b.DayTime < '2007-01-02 05:00:00.000'
GROUP BY DATE(b.DayTime)

如果你删除了那里,你应该从所有日子开始。

SELECT DATE(b.DayTime),sum(b.kwh) as consumption
FROM BATimeSeries b
WHERE b.companyNo = 1 and 
GROUP BY DATE(b.DayTime)