如何查找一年中以前的金额之和,包括当前的金额

时间:2019-07-09 16:05:57

标签: sql-server sum

我需要获取第二个条目的当前总和与该特定年份的先前金额。

Input_table

+-----------+----------+-------------+-----------+----------+
| ID        | Name     | Date1       |Date 2     |amount    |
+-----------+----------+-------------+-----------+----------+
| 10000000  | ABC      | 11/2/2017   |**11/2/2018**  |2504      |
| 10000000  | ABC      | 12/20/2017  |**11/2/2018**  |-2174     |
| 10000000  | ABC      | 10/05/2018  |10/05/2019 |1234      |
| 10000000  | ABC      | 10/06/2019  |10/06/2020 |3456      |
+-----------+----------+-------------+-----------+----------+

这是所需的输出:

+-----------+----------+-------------+-----------+----------+
| ID        | Name     | Date1       |Date 2     |amount    |
+-----------+----------+-------------+-----------+----------+
| 10000000  | ABC      | 11/2/2017   |**11/2/2018**  |2504      |
| 10000000  | ABC      | 12/20/2017  |**11/2/2018**  |329       |
| 10000000  | ABC      | 10/05/2018  |10/05/2019 |1234      |
| 10000000  | ABC      | 10/06/2019  |10/06/2020 |3456      |
+-----------+----------+-------------+-----------+----------+

记录3的金额-1234(1234 + 0(该年没有记录)),因为日期2不同

记录4的金额-3456(3456 + 0(该年没有记录)),因为日期2不同

如果两个记录的日期2相同,我正在寻找记录的当前,先前金额的总和。假设我们有3个记录的日期2值相同。那么第一条记录应具有其实际值,第二条记录应具有第一条记录量+ 2条记录量,第三条记录应具有第一条记录量+第二条记录量+第三条记录量。

1 个答案:

答案 0 :(得分:1)

我在这里做了一些相当大的假设。主要是想查看当前记录的“ Date2”值是否存在于上一年的“ Date1”中(例如,Date2:11/02/2018正在查看Date1:​​11/02/2017是否存在),并且< strong> ID 和 Name 列在过滤数据方面具有某种相关性。

这是我在SSMS中使用的东西:

-- Create sample data.

DECLARE @Data TABLE (
    ID VARCHAR(10), [Name] VARCHAR(3), Date1 DATE, Date2 DATE, Amount DECIMAL(18,2)
);

INSERT INTO @Data ( ID, [Name], Date1, Date2, Amount ) VALUES
( '10000000', 'ABC', '11/2/2017', '11/2/2018',  2504),
( '10000000', 'ABC', '12/20/2017', '11/2/2018',  -2174),
( '10000000', 'ABC', '10/05/2018', '10/05/2019',  1234),
( '10000000', 'ABC', '10/06/2019', '10/06/2020',  3456);

-- Query sample data

SELECT
    MyData.ID, MyData.[Name], MyData.Date1, MyData.Date2, MyData.Amount
    , ISNULL( LastYear.Amount, 0 ) AS PreviousYear
    , ( MyData.Amount + ISNULL( LastYear.Amount, 0 ) ) AS NewAmount
FROM @Data AS MyData
OUTER APPLY (

    SELECT
        SubData.Amount AS Amount
    FROM @Data AS SubData
    WHERE
        SubData.ID = MyData.ID
        AND SubData.[Name] = MyData.[Name]
        AND SubData.Date1 = DATEADD( yy, -1, MyData.Date2 )
        AND SubData.Date1 <> MyData.Date1   -- A weak attempt to exclude the current MyData record. Really needs a unique id.

) AS LastYear
ORDER BY
    MyData.Date2;

-返回

+----------+------+------------+------------+----------+--------------+-----------+
|    ID    | Name |   Date1    |   Date2    |  Amount  | PreviousYear | NewAmount |
+----------+------+------------+------------+----------+--------------+-----------+
| 10000000 | ABC  | 2017-11-02 | 2018-11-02 | 2504.00  | 0.00         | 2504.00   |
| 10000000 | ABC  | 2017-12-20 | 2018-11-02 | -2174.00 | 2504.00      | 330.00    |
| 10000000 | ABC  | 2018-10-05 | 2019-10-05 | 1234.00  | 0.00         | 1234.00   |
| 10000000 | ABC  | 2019-10-06 | 2020-10-06 | 3456.00  | 0.00         | 3456.00   |
+----------+------+------------+------------+----------+--------------+-----------+

理想情况下,查询表具有一个PK,可用于从当前记录中排除该记录(请注意尝试使用Date1的微弱尝试)。

使用PK(唯一ID)的同一示例:

DECLARE @Data TABLE (
    ID VARCHAR(10), [Name] VARCHAR(3), Date1 DATE, Date2 DATE, Amount DECIMAL(18,2), pk_PrimaryKey INT IDENTITY(1,1) PRIMARY KEY
);

INSERT INTO @Data ( ID, [Name], Date1, Date2, Amount ) VALUES
( '10000000', 'ABC', '11/2/2017', '11/2/2018',  2504),
( '10000000', 'ABC', '12/20/2017', '11/2/2018',  -2174),
( '10000000', 'ABC', '10/05/2018', '10/05/2019',  1234),
( '10000000', 'ABC', '10/06/2019', '10/06/2020',  3456);

SELECT
    MyData.ID, MyData.[Name], MyData.Date1, MyData.Date2, MyData.Amount
    , ISNULL( LastYear.Amount, 0 ) AS PreviousYear
    , ( MyData.Amount + ISNULL( LastYear.Amount, 0 ) ) AS NewAmount
FROM @Data AS MyData
OUTER APPLY (

    SELECT
        SubData.Amount AS Amount
    FROM @Data AS SubData
    WHERE
        SubData.ID = MyData.ID
        AND SubData.[Name] = MyData.[Name]
        AND SubData.Date1 = DATEADD( yy, -1, MyData.Date2 )
        AND SubData.pk_PrimaryKey <> MyData.pk_PrimaryKey

) AS LastYear
ORDER BY
    MyData.Date1;

无论如何,这是我基于必须提供的少量信息而得出的最佳猜测。希望这可以使您朝正确的方向前进。