Get gas consumption from aggregate data

This is my first post! I have been dealing with this issue for a while now and I really need your help.

I have 2 tables

Counters
=============   
ID  Description
-------------------
1   GasCounter1
2   GasCounter2

Measurements
================
ID  TimeStamp              ValCum   CounterID
----------------------------------------------
1   01.01.2015 00:00:00    1        1
2   01.01.2015 00:15:00    2        1
3   01.01.2015 00:30:00    3        1
4   01.01.2015 00:45:00    4        1
---------------------------------------
5   01.01.2015 01:00:00    5        1
6   01.01.2015 01:15:00    6        1
7   01.01.2015 01:30:00    7        1
8   01.01.2015 01:45:00    8        1
---------------------------------------
9   01.01.2015 02:00:00    9        1
10  01.01.2015 02:15:00    10       1
11  01.01.2015 02:30:00    11       1
12  01.01.2015 02:45:00    12       1
---------------------------------------
13  01.01.2015 03:00:00    13       1
14  01.01.2015 03:15:00    14       1
15  01.01.2015 03:30:00    15       1
16  01.01.2015 03:45:00    16       1
---------------------------------------
17  01.01.2015 04:00:00    17       1
18  01.01.2015 04:15:00    18       1
19  01.01.2015 04:30:00    19       1
20  01.01.2015 04:45:00    20       1

.... Here are data only for GasCounter1

      

ValueCum is the cumulative flow rate read from GasCounters

I need to calculate the gas consumption for each hour (from the selected interval) In this example, all costs are 4 .

1->2 = 1 +
2->3 = 1 +
3->4 = 1 +
4->5 = 1 = ===> 4

5->6 = 1
6->7 = 1
7->8 = 1
8->9 = 1 ===> 4

      

If the actual data were:

5->6 = 1
6->7 = 1
7->9 = 2
9->12 = 3 ===> 7

      

Well I tried with Group by -> I can calculate the sums of the groups, but it is wrong. While I was searching, I found some solution to do the grand total and the grand total , but this is correct as I don't know what I need.

I already have cumulative values ​​-> I need the difference between:

last record in **previous** group and 
last record inside current group

      

Hope I figured out the problem. Appreciate your help

+3


source to share


1 answer


If I get your question correctly, this might be what you need.



-- Generate demo data
CREATE TABLE #measure(id int identity(1,1), [TimeStamp] datetime, valcum int, counterid int)

INSERT INTO #measure(TimeStamp, valcum, counterid)
VALUES  (DATEADD(hour,-3,GETDATE()),1,1), (DATEADD(hour,-3,GETDATE()),2,1), (DATEADD(hour,-3,GETDATE()),1,1), (DATEADD(hour,-3,GETDATE()),2,1),
        (DATEADD(hour,-2,GETDATE()),8,1), (DATEADD(hour,-2,GETDATE()),1,1), (DATEADD(hour,-2,GETDATE()),8,1), (DATEADD(hour,-2,GETDATE()),1,1), 
        (DATEADD(hour,-1,GETDATE()),3,1), (DATEADD(hour,-1,GETDATE()),1,1), (DATEADD(hour,-1,GETDATE()),3,1), (DATEADD(hour,-1,GETDATE()),1,1),
        (GETDATE(),2,1), (GETDATE(),1,1), (GETDATE(),10,1), (GETDATE(),2,1), (GETDATE(),1,1), (GETDATE(),10,1)

-- Show demo data
SELECT *
FROM #measure

-- Sum every value grouped per date and hour.
;WITH data AS(
    SELECT id, TimeStamp, valcum, counterid, 
        SUM(valcum) OVER (PARTITION BY  CONVERT(date, m.timestamp), DATEPART(hour, m.TimeStamp)) as sumVal,
        DENSE_RANK() OVER (ORDER BY CONVERT(date, m.timestamp), DATEPART(hour, m.TimeStamp)) as package,
        ROW_NUMBER() OVER (PARTITION BY  CONVERT(date, m.timestamp), DATEPART(hour, m.TimeStamp) ORDER BY id) as numberInPackage
    FROM #measure as m
)
SELECT *, d1.valcum - d2.valcum as diff
FROM data as d1
LEFT JOIN data as d2
        ON d1.package-1 = d2.package
WHERE d1.numberInPackage = 4 and d2.numberInPackage = 4


-- Cleanup
DROP TABLE #measure

      

+1


source







All Articles