I want to find the peak value across multiple time series concurrently, like the following. However to make this a little more complicated, I may have 20k+ entities with 200 data points each, so performance is important.
I only require the peak achieved across all entities. So for clarification, let's say my entities are stores and my value is total sales per day, and I want to find out which day was the peak sales day across all 20k stores.
Sample data
Date Time Entity Value
01/01/1900 A 8
01/01/1900 B 6
01/01/1900 C 9
02/01/1900 A 4
02/01/1900 B 3
02/01/1900 C 6
03/01/1900 A 7
03/01/1900 B 8
03/01/1900 C 9
04/01/1900 A 1
04/01/1900 B 2
04/01/1900 C 5
OUTPUT
Date Time A B C Total
01/01/1900 8 6 9 23
02/01/1900 4 3 6 13
03/01/1900 7 8 9 24
04/01/1900 1 2 5 8
However this output is not important, I simple require the peak DateTime and total.
Date Time Total
03/01/1900 24
答案 0 :(得分:2)
如果您只需要最大总和的日期,那么简单的GROUP BY
就足够了。
SELECT TOP(1)
dt
,SUM(Value) AS Total
FROM T
GROUP BY dt
ORDER BY Total DESC;
<强>结果强>
dt Total
1900-01-03 24
答案 1 :(得分:0)
此处使用动态查询解决方案
-- create temp table for data sample
IF OBJECT_ID('Tempdb..#test') IS NOT NULL
DROP TABLE #test
CREATE TABLE #test
(
[Date Time] DATE ,
Entity VARCHAR(1) ,
VALUE INT
)
INSERT INTO #test
( [Date Time], Entity, VALUE )
VALUES ( '1900-01-01', 'A', 4 ),
( '1900-01-01', 'B', 6 ),
( '1900-01-01', 'C', 9 ),
( '1900-01-02', 'A', 4 ),
( '1900-01-02', 'B', 3 ),
( '1900-01-02', 'C', 6 ),
( '1900-01-03', 'A', 7 ),
( '1900-01-03', 'B', 8 ),
( '1900-01-03', 'C', 9 ),
( '1900-01-04', 'A', 1 ),
( '1900-01-04', 'B', 2 ),
( '1900-01-04', 'C', 5 )
--final query
DECLARE @String AS NVARCHAR(MAX)
SET @String = SUBSTRING(( SELECT ',[' + CONVERT(VARCHAR, Entity) + ']'
FROM ( SELECT DISTINCT
Entity
FROM #test
) AS t
ORDER BY Entity
FOR
XML PATH('')
), 2, 1000)
SET @String = 'SELECT *, ' + REPLACE(@String, ',', '+') + ' AS Total '
+ 'FROM (SELECT * FROM #test AS [T] PIVOT ( MAX(T.VALUE) FOR [T].[Entity] IN ('
+ @String + ') ) AS P) AS T'
EXEC sys.sp_executesql @String
测试在这里 SQL Fiddle
答案 2 :(得分:0)
试试这个
SELECT Datetime,A,B,C,Total=SUM([A]+[B]+[C])
FROM Table
PIVOT
(SUM(value) FOR entity in ([A],[B],[C])) AS P
GROUP BY Datetime,A,B,C
答案 3 :(得分:0)
您可以使用此代码块:
select t.date_time
,sum(t.value) as value
from test t
group by t.date_time
order by t.date_time
它给出了这个输出: