获得上一年的同一季度收入

问题描述 投票:0回答:2

我有一组数据,该数据按部门,会计年度,会计季度和最终总金额划分,如下所示。

+---------------------------------+------------+---------------+-------------+
|            Department           | FISCALYEAR | FISCALQUARTER | TotalAmount |
+---------------------------------+------------+---------------+-------------+
| Internal Medicine - Dermatology |       2018 |             2 | 50.00       |
| Internal Medicine - Dermatology |       2018 |             4 | 75.00       |
| Internal Medicine - Dermatology |       2019 |             1 | 135.00      |
| Internal Medicine - Dermatology |       2019 |             2 | 75.00       |
| Internal Medicine - Dermatology |       2019 |             3 | 185.00      |
| Internal Medicine - Dermatology |       2019 |             4 | 84.00       |
| Internal Medicine - Dermatology |       2020 |             1 | 85.00       |
| Internal Medicine - Dermatology |       2020 |             2 | 10.00       |
+---------------------------------+------------+---------------+-------------+

我将如何添加一列以获取上一年/季度的总金额?例如,2020财年,第二财季将显示75.00。

困难的部分是某些季度没有任何数据,因此存在差距。

我已经尝试了LAG()窗口函数,但是由于它不是标准偏移量,因此很难指定偏移量。

任何帮助/想法都很棒。

sql sql-server sql-server-2017
2个回答
1
投票

this怎么样?

IF OBJECT_ID('tempdb.dbo.#YourTable', 'U') IS NOT NULL DROP TABLE #YourTable; 

CREATE TABLE #YourTable(
   Department    VARCHAR(33) NOT NULL
  ,FISCALYEAR    INTEGER  NOT NULL
  ,FISCALQUARTER INTEGER  NOT NULL
  ,TotalAmount   NUMERIC(7,2) NOT NULL
);

INSERT INTO #YourTable(Department,FISCALYEAR,FISCALQUARTER,TotalAmount) VALUES ('Internal Medicine - Dermatology',2018,2,50.00);
INSERT INTO #YourTable(Department,FISCALYEAR,FISCALQUARTER,TotalAmount) VALUES ('Internal Medicine - Dermatology',2018,4,75.00);
INSERT INTO #YourTable(Department,FISCALYEAR,FISCALQUARTER,TotalAmount) VALUES ('Internal Medicine - Dermatology',2019,1,135.00);
INSERT INTO #YourTable(Department,FISCALYEAR,FISCALQUARTER,TotalAmount) VALUES ('Internal Medicine - Dermatology',2019,2,75.00);
INSERT INTO #YourTable(Department,FISCALYEAR,FISCALQUARTER,TotalAmount) VALUES ('Internal Medicine - Dermatology',2019,3,185.00);
INSERT INTO #YourTable(Department,FISCALYEAR,FISCALQUARTER,TotalAmount) VALUES ('Internal Medicine - Dermatology',2019,4,84.00);
INSERT INTO #YourTable(Department,FISCALYEAR,FISCALQUARTER,TotalAmount) VALUES ('Internal Medicine - Dermatology',2020,1,85.00);
INSERT INTO #YourTable(Department,FISCALYEAR,FISCALQUARTER,TotalAmount) VALUES ('Internal Medicine - Dermatology',2020,2,10.00);

SELECT a.Department, a.FISCALYEAR, a.FISCALQUARTER, 
       a.TotalAmount, b.TotalAmount AS PriorYearQuarterTotalAmount
FROM #YourTable a
LEFT JOIN #YourTable b ON a.Department = b.Department
                     AND a.FISCALYEAR - 1 = b.FISCALYEAR
                     AND a.FISCALQUARTER = b.FISCALQUARTER

0
投票

您可以使用内联查询:

select 
    t.*,
    (
        select t1.TotalAmount 
        from mytable t1 
        where t1.FiscalQuarter = t.FiscalQuarter and t1.FiscalYear = t.FiscalYear - 1
    ) lastTotalAmount
from mytable t
© www.soinside.com 2019 - 2024. All rights reserved.