如何找到SQL Server季度季度总收入?

时间:2019-02-01 07:46:38

标签: sql sql-server tsql sql-server-2008

我已经对平面数据进行了编码,并获得了最佳结果。现在,我想找到四个季度的总收入。

数据集:

Symbol Date Quarter Type Revenue
---------------------------------
A1 12/31/2018 4q2018 x1 5000
B1 12/31/2018 4q2018 x2 2000
C1 12/31/2018 4q2018 x3 3500
D1 12/31/2018 4q2018 x4 2000
A1 09/30/2018 3q2018 x1 1000
B1 09/30/2018 3q2018 x2 2000
C1 09/30/2018 3q2018 x3 1500
D1 09/30/2018 3q2018 x4 2500
A1 06/30/2018 2q2018 x1 1000
B1 06/24/2018 2q2018 x2 4000
C1 06/30/2018 2q2018 x3 1000
D1 06/30/2018 2q2018 x4 2000
A1 03/31/2018 1q2018 x1 1000
B1 01/28/2018 1q2018 x2 4000
C1 02/21/2018 1q2018 x3 1000
D1 03/31/2018 1q2018 x4 2500
A1 12/31/2017 4q2017 x1 1000
B1 12/31/2017 4q2017 x2 2000
C1 12/31/2017 4q2017 x3 1500
D1 12/31/2017 4q2017 x4 2500

预期输出:

5th row: A1 4q2018 x1 8000 (sum of previous 4 quarters)
6th row: A1 4q2018 x1 12000 (sum of previous 4 quarters)
7th row: A1 4q2018 x1 7500 (sum of previous 4 quarters)
8th row: A1 4q2018 x1 9000 (sum of previous 4 quarters)

再进行4条记录后的同一件事

13th row: A1 3q2018 x1 4000 (sum of 3q, 2q, 1q 2018 and 4q 2017) 

2 个答案:

答案 0 :(得分:1)

您将需要使用OVER子句。可能类似

select Symbol, [Quarter], SUM(Revenue) OVER (PARTITION BY Symbol
                                     ORDER BY [Date]
                                     ROWS 4 PRECEDING) AS total

对此付出代价,我想您会接近的。另请在此处阅读文档:https://docs.microsoft.com/en-us/sql/t-sql/queries/select-over-clause-transact-sql?view=sql-server-2017

答案 1 :(得分:1)

Sean在上面的代码中做了一点改动,而他的代码添加了所有行,从而解决了该问题:

CREATE TABLE #t (Symbol varchar(5),Date datetime,Quarter varchar(10),Type varchar(2),Revenue int)
GO
INSERT INTO #t (Symbol,Date,Quarter,Type,Revenue) VALUES('A1','12/31/2018','4q2018','x1',5000)
INSERT INTO #t (Symbol,Date,Quarter,Type,Revenue) VALUES('B1','12/31/2018','4q2018','x2',2000)
INSERT INTO #t (Symbol,Date,Quarter,Type,Revenue) VALUES('C1','12/31/2018','4q2018','x3',3500)
INSERT INTO #t (Symbol,Date,Quarter,Type,Revenue) VALUES('D1','12/31/2018','4q2018','x4',2000)
INSERT INTO #t (Symbol,Date,Quarter,Type,Revenue) VALUES('A1','09/30/2018','3q2018','x1',1000)
INSERT INTO #t (Symbol,Date,Quarter,Type,Revenue) VALUES('B1','09/30/2018','3q2018','x2',2000)
INSERT INTO #t (Symbol,Date,Quarter,Type,Revenue) VALUES('C1','09/30/2018','3q2018','x3',1500)
INSERT INTO #t (Symbol,Date,Quarter,Type,Revenue) VALUES('D1','09/30/2018','3q2018','x4',2500)
INSERT INTO #t (Symbol,Date,Quarter,Type,Revenue) VALUES('A1','06/30/2018','2q2018','x1',1000)
INSERT INTO #t (Symbol,Date,Quarter,Type,Revenue) VALUES('B1','06/24/2018','2q2018','x2',4000)
INSERT INTO #t (Symbol,Date,Quarter,Type,Revenue) VALUES('C1','06/30/2018','2q2018','x3',1000)
INSERT INTO #t (Symbol,Date,Quarter,Type,Revenue) VALUES('D1','06/30/2018','2q2018','x4',2000)
INSERT INTO #t (Symbol,Date,Quarter,Type,Revenue) VALUES('A1','03/31/2018','1q2018','x1',1000)
INSERT INTO #t (Symbol,Date,Quarter,Type,Revenue) VALUES('B1','01/28/2018','1q2018','x2',4000)
INSERT INTO #t (Symbol,Date,Quarter,Type,Revenue) VALUES('C1','02/21/2018','1q2018','x3',1000)
INSERT INTO #t (Symbol,Date,Quarter,Type,Revenue) VALUES('D1','03/31/2018','1q2018','x4',2500)
INSERT INTO #t (Symbol,Date,Quarter,Type,Revenue) VALUES('A1','12/31/2017','4q2017','x1',1000)
INSERT INTO #t (Symbol,Date,Quarter,Type,Revenue) VALUES('B1','12/31/2017','4q2017','x2',2000)
INSERT INTO #t (Symbol,Date,Quarter,Type,Revenue) VALUES('C1','12/31/2017','4q2017','x3',1500)
INSERT INTO #t (Symbol,Date,Quarter,Type,Revenue) VALUES('D1','12/31/2017','4q2017','x4',2500)

--SELECT * FROM #t WHERE Symbol = 'B1' ORDER BY Date Desc
select Symbol, [Quarter], SUM(Revenue) OVER (PARTITION BY Symbol
                                     ORDER BY [Date]
                                     ROWS 3 PRECEDING) AS total,Date
FROM #t
ORDER BY Date Desc

这是sql server 2008的另一个更新:

    select a.Symbol, a.[Quarter], x.Rev,a.Date
FROM #t a 
CROSS APPLY (   SELECT ISNULL(SUM(re), 0) Rev
                    FROM (  SELECT TOP(4) b.Revenue re
                            FROM #t b
                            WHERE b.Symbol = a.Symbol and Convert(varchar,b.Date,112) <= Convert(varchar,a.Date,112)
                            ORDER BY b.Date DESC ) v
                ) x
ORDER BY a.Date Desc

    DROP TABLE #t