gpt4 book ai didi

sql - 从累积数据中获取耗气量

转载 作者:行者123 更新时间:2023-12-01 07:44:40 25 4
gpt4 key购买 nike

这是我在这里的第一篇文章!我处理这个查询已经有一段时间了,我真的需要你的帮助。

我有两张 table

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

好吧,我尝试使用 Group by --> 我可以计算组总和,但那是错误的。当我四处搜索时,我找到了一些running totalcumulative 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

关于sql - 从累积数据中获取耗气量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30862431/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com