简体   繁体   English

SQL Server SUM(值)

[英]SQL Server SUM(Values)

I have the following query that works perfectly well. 我有以下查询,效果很好。 The query sums the values in a given day. 查询将给定日期的值相加。

SELECT 
    SUM(fldValue) AS 'kWh',
    DAY(fldDateTime) AS 'Day',
    MONTH(fldDateTime) AS 'Month',
    YEAR(fldDateTime) AS 'Year'            
FROM 
    [Data.tblData]
WHERE 
    tblData_Id IN (SELECT DISTINCT tblData_Id 
                   FROM [Data.tblData])
GROUP BY    
    YEAR(fldDateTime), MONTH(fldDateTime), DAY(fldDateTime),
    tblData_Id,fldDateTime
ORDER BY 
    YEAR(fldDateTime), MONTH(fldDateTime), DAY(fldDateTime)

The problem I have is that it sums from midnight to midnight, I need it to sum the values after midnight ( >= Midnight) then up to midnight of the next day. 我的问题是它从午夜到午夜求和,我需要它在午夜(> =午夜)之后到第二天午夜之前求和。 The reason for this is the data that comes in for a day, is always after midnight. 这样做的原因是一天中输入的数据总是在午夜之后。 For example the first logged data will be '2016-01-01 00:01:00', the final logged data will be '2016-01-02 00:00:00'. 例如,第一个记录的数据将为“ 2016-01-01 00:01:00”,最后一个记录的数据将为“ 2016-01-02 00:00:00”。 This is how the hardware works that sends me the data. 这就是向我发送数据的硬件的工作方式。

I would like to know how to encapsulate >= midnight to midnight in the query. 我想知道如何在查询中封装> =午夜到午夜。

Dataset: 资料集:

DateTime    Value
20/03/2016 00:30    69.00
20/03/2016 01:00    69.00
20/03/2016 01:30    69.00
20/03/2016 02:00    69.00
20/03/2016 02:30    69.00
20/03/2016 03:00    69.00
20/03/2016 03:30    11.88
20/03/2016 04:00    0.52
20/03/2016 04:30    1.51
20/03/2016 05:00    2.22
20/03/2016 05:30    2.11
20/03/2016 06:00    0.05
20/03/2016 06:30    6.78
20/03/2016 07:00    14.79
20/03/2016 07:30    1.57
20/03/2016 08:00    1.51
20/03/2016 08:30    4.81
20/03/2016 09:00    0.11
20/03/2016 09:30    8.99
20/03/2016 10:00    10.06
20/03/2016 10:30    15.28
20/03/2016 11:00    3.22
20/03/2016 11:30    1.73
20/03/2016 12:00    19.10
20/03/2016 12:30    2.08
20/03/2016 13:00    2.61
20/03/2016 13:30    0.84
20/03/2016 14:00    8.65
20/03/2016 14:30    2.37
20/03/2016 15:00    16.34
20/03/2016 15:30    12.66
20/03/2016 16:00    2.64
20/03/2016 16:30    0.19
20/03/2016 17:00    3.91
20/03/2016 17:30    2.39
20/03/2016 18:00    0.57
20/03/2016 18:30    1.30
20/03/2016 19:00    5.06
20/03/2016 19:30    17.45
20/03/2016 20:00    13.04
20/03/2016 20:30    5.00
20/03/2016 21:00    7.47
20/03/2016 21:30    5.09
20/03/2016 22:00    0.33
20/03/2016 22:30    5.29
20/03/2016 23:00    15.33
20/03/2016 23:30    5.39
21/03/2016 00:00    6.74

Thank you in advance. 先感谢您。

The expected sum output value for 20/03/2016 is: 662.98 2016年3月20日的预期总和输出值为:662.98

The output table will look like: 输出表如下所示:

SumValue    Day Month   Year    Meter Id
659.18  20  3   2016    6
251.37  21  3   2016    6
279.03  22  3   2016    6
280.03  23  3   2016    6
284.22  24  3   2016    6
310.12  25  3   2016    6
320.84  26  3   2016    6
269.29  27  3   2016    6
276.11  28  3   2016    6
279.11  29  3   2016    6

The value column is the sum of the values for that day, made up of lots of individual times. 值列是当天的值总和,由很多次独立时间组成。

First, I have no idea what the WHERE clause is doing, so I'm going to remove it. 首先,我不知道WHERE子句在做什么,因此我将其删除。

Second, don't use single quotes for column names. 其次,不要对列名使用单引号。

Third, your GROUP BY clause is too complicated. 第三,您的GROUP BY子句太复杂了。 You only need to include the unaggregated columns in the SELECT . 您只需要在SELECT包括未聚合的列。

Finally, the key idea is to subtract one hour from the values everywhere they are used. 最后,关键思想是在所有使用值的地方减去一小时。 Here is a simple method: 这是一个简单的方法:

SELECT SUM(fldValue) AS kWh,
       DAY(newdt) AS [Day],
       MONTH(newdt) AS [Month],
       YEAR(newdt) AS [Year]           
FROM (SELECT d.*, DATEADD(hour, -1, fldDateTime) as newdt
      FROM Data.tblData d
     ) d
GROUP BY YEAR(newdt), MONTH(newdt), DAY(newdt)
ORDER BY YEAR(newdt), MONTH(newdt), DAY(newdt)

Same answer as @Gordon but you can subtract one minute instead of one hour. 答案与@Gordon相同,但您可以减去一分钟而不是一小时。

SELECT SUM(fldValue) AS kWh,
   DAY(newdt) AS [Day],
   MONTH(newdt) AS [Month],
   YEAR(newdt) AS [Year]           
FROM (SELECT d.*, DATEADD(minute, -1, fldDateTime) as newdt
  FROM Data.tblData d
 ) d
GROUP BY YEAR(newdt), MONTH(newdt), DAY(newdt)
ORDER BY YEAR(newdt), MONTH(newdt), DAY(newdt)
declare @tempTable table ([DateTime] datetime, Value Float)

insert into @tempTable ([DateTime], [Value])
select convert(datetime,'20/03/2016 00:30',103), 69.00 union all
select convert(datetime,'20/03/2016 01:00',103), 69.00 union all
select convert(datetime,'21/03/2016 00:00',103), 6.74

select * from @tempTable

select [sum] = SUM(value), [year] = year(DT), [month] = month(DT), [day] = day(DT)
from (select Value, DT = dateadd(second, -1, [DateTime]) from @tempTable) x
group by year(DT), month(DT), day(DT)

Use the below query for summing up the midnight value with previous day. 使用以下查询将前一天的午夜值相加。

SELECT 
    SUM(fldValue) AS 'kWh',
    CASE WHEN CONVERT(VARCHAR(8), fldDateTime, 108)='00:00:00' THEN DAY(fldDateTime)-1 ELSE DAY(fldDateTime) END AS 'Day',
    MONTH(fldDateTime) AS 'Month',
    YEAR(fldDateTime) AS 'Year'            
FROM 
    Data.[tblData]
GROUP BY    
    YEAR(fldDateTime), MONTH(fldDateTime),CASE WHEN CONVERT(VARCHAR(8), fldDateTime, 108)='00:00:00' THEN DAY(fldDateTime)-1 ELSE DAY(fldDateTime) END

ORDER BY 
    YEAR(fldDateTime), MONTH(fldDateTime), CASE WHEN CONVERT(VARCHAR(8), fldDateTime, 108)='00:00:00' THEN DAY(fldDateTime)-1 ELSE DAY(fldDateTime) END

Sample output : 样本输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM