简体   繁体   中英

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'. 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

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.

Second, don't use single quotes for column names.

Third, your GROUP BY clause is too complicated. You only need to include the unaggregated columns in the 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.

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 :

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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