从累积数据中获取耗气量
Get gas consumption from cumulative data
这是我第一次post来这里!
我处理这个问题已经有一段时间了,我真的需要你的帮助。
我有2张桌子
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 是 累计 从 GasCounters
读取的消耗量
我需要计算每小时的耗气量(从选定的时间间隔开始)
在这个例子中所有的消费都是 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
如果实际数据是:
5->6 = 1
6->7 = 1
7->9 = 2
9->12 = 3 ===> 7
好吧,我尝试使用分组依据 --> 我可以计算分组总和,但这是错误的。
当我在 google-ing 周围时,我找到了 运行 total 和 cumulative sum 的一些解决方案,但这是正确的我需要的相反。
我已经有了累计值 -> 我需要两者之间的差值:
last record in **previous** group and
last record inside current group
我希望我清楚这个问题。
感谢您的帮助
如果我答对了你的问题,也许这就是你要找的东西。
-- 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
这是我第一次post来这里! 我处理这个问题已经有一段时间了,我真的需要你的帮助。
我有2张桌子
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 是 累计 从 GasCounters
读取的消耗量我需要计算每小时的耗气量(从选定的时间间隔开始) 在这个例子中所有的消费都是 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
如果实际数据是:
5->6 = 1
6->7 = 1
7->9 = 2
9->12 = 3 ===> 7
好吧,我尝试使用分组依据 --> 我可以计算分组总和,但这是错误的。 当我在 google-ing 周围时,我找到了 运行 total 和 cumulative sum 的一些解决方案,但这是正确的我需要的相反。
我已经有了累计值 -> 我需要两者之间的差值:
last record in **previous** group and
last record inside current group
我希望我清楚这个问题。 感谢您的帮助
如果我答对了你的问题,也许这就是你要找的东西。
-- 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