计算最近12个月+过去12个月之前所有交易的总和

时间:2013-09-12 04:55:30

标签: sql sql-server-2005

想知道你是否可以帮我查询一下?

在金融行业工作,我的sql有点生疏。我需要返回一些我们需要传输到另一个数据库的数据。

出于测试原因,我将下表放在一起。

我有2个帐户,1和2,每个帐户有2年的交易。

我的尝试

    SELECT TOP 12 
    tt.AccountNo,
    tt.Amount,
    tt.CreatedDate,
    --Get Sum(Amount) of all transaction  before last 12 month
    Startingbalance=(SELECT sum(Amount)
                   FROM TransactionsTest tt2 
                  WHERE tt.AccountNo=tt2.AccountNo
                  AND --exclude last 12 months)
    FROM    TransactionsTest tt
    --WHERE tt.AccountNo=1
    GROUP BY tt.AccountNo,tt.Amount,tt.CreatedDate
    ORDER BY tt.CreatedDate desc

任务:

返回每个帐户的最后12笔交易+每个帐户必须返回“StartingBalance” StartingBalance =过去12个月之前所有交易的总和(金额)。

我有什么建议吗?

非常感谢

        BEGIN TRANSACTION;
    IF object_id(N'TransactionsTest', 'U') IS NOT NULL
        DROP TABLE TransactionsTest
    GO

    CREATE TABLE [dbo].[TransactionsTest](
        [Id] [int] NOT NULL,
        [AccountNo] [int] NOT NULL,
        [Amount] [decimal](18, 2) NOT NULL,
        [CreatedDate] [datetime] NOT NULL
    ) ON [PRIMARY]

    GO

    INSERT INTO [dbo].[TransactionsTest]([Id], [AccountNo], [Amount], [CreatedDate])
    SELECT 1, 1, 10.00, '20120128 00:00:00.000' UNION ALL
    SELECT 2, 1, 10.00, '20120228 00:00:00.000' UNION ALL
    SELECT 3, 1, 10.00, '20120328 00:00:00.000' UNION ALL
    SELECT 4, 1, 10.00, '20120428 00:00:00.000' UNION ALL
    SELECT 5, 1, 10.00, '20120528 00:00:00.000' UNION ALL
    SELECT 6, 1, 10.00, '20120628 00:00:00.000' UNION ALL
    SELECT 7, 1, 10.00, '20120728 00:00:00.000' UNION ALL
    SELECT 8, 1, 10.00, '20120828 00:00:00.000' UNION ALL
    SELECT 9, 1, 10.00, '20120928 00:00:00.000' UNION ALL
    SELECT 10, 1, 10.00, '20121028 00:00:00.000' UNION ALL
    SELECT 11, 1, 10.00, '20121128 00:00:00.000' UNION ALL
    SELECT 12, 1, 10.00, '20121228 00:00:00.000' UNION ALL
    SELECT 13, 1, 10.00, '20130128 00:00:00.000' UNION ALL
    SELECT 14, 1, 10.00, '20130228 00:00:00.000' UNION ALL
    SELECT 15, 1, 10.00, '20130328 00:00:00.000' UNION ALL
    SELECT 16, 1, 10.00, '20130428 00:00:00.000' UNION ALL
    SELECT 17, 1, 10.00, '20130528 00:00:00.000' UNION ALL
    SELECT 18, 1, 10.00, '20130628 00:00:00.000' UNION ALL
    SELECT 19, 1, 10.00, '20130728 00:00:00.000' UNION ALL
    SELECT 20, 1, 10.00, '20130828 00:00:00.000' UNION ALL
    SELECT 21, 1, 10.00, '20130928 00:00:00.000' UNION ALL
    SELECT 22, 1, 10.00, '20131028 00:00:00.000' UNION ALL
    SELECT 23, 1, 10.00, '20131128 00:00:00.000' UNION ALL
    SELECT 24, 1, 10.00, '20131228 00:00:00.000' UNION ALL
    SELECT 25, 2, 20.00, '20120128 00:00:00.000' UNION ALL
    SELECT 26, 2, 20.00, '20120228 00:00:00.000' UNION ALL
    SELECT 27, 2, 20.00, '20120328 00:00:00.000' UNION ALL
    SELECT 28, 2, 20.00, '20120428 00:00:00.000' UNION ALL
    SELECT 29, 2, 20.00, '20120528 00:00:00.000' UNION ALL
    SELECT 30, 2, 20.00, '20120628 00:00:00.000' UNION ALL
    SELECT 31, 2, 20.00, '20120728 00:00:00.000' UNION ALL
    SELECT 32, 2, 20.00, '20120828 00:00:00.000' UNION ALL
    SELECT 33, 2, 20.00, '20120928 00:00:00.000' UNION ALL
    SELECT 34, 2, 20.00, '20121028 00:00:00.000' UNION ALL
    SELECT 35, 2, 20.00, '20121128 00:00:00.000' UNION ALL
    SELECT 36, 2, 20.00, '20121228 00:00:00.000' UNION ALL
    SELECT 37, 2, 20.00, '20130128 00:00:00.000' UNION ALL
    SELECT 38, 2, 20.00, '20130228 00:00:00.000' UNION ALL
    SELECT 39, 2, 20.00, '20130328 00:00:00.000' UNION ALL
    SELECT 40, 2, 20.00, '20130428 00:00:00.000' UNION ALL
    SELECT 41, 2, 20.00, '20130528 00:00:00.000' UNION ALL
    SELECT 42, 2, 20.00, '20130628 00:00:00.000' UNION ALL
    SELECT 43, 2, 20.00, '20130728 00:00:00.000' UNION ALL
    SELECT 44, 2, 20.00, '20130828 00:00:00.000' UNION ALL
    SELECT 45, 2, 20.00, '20130928 00:00:00.000' UNION ALL
    SELECT 46, 2, 20.00, '20131028 00:00:00.000' UNION ALL
    SELECT 47, 2, 20.00, '20131128 00:00:00.000' UNION ALL
    SELECT 48, 2, 20.00, '20131228 00:00:00.000'
    COMMIT;

1 个答案:

答案 0 :(得分:0)

with cte as (
    select
        row_number() over(partition by AccountNo order by CreatedDate desc) as rn,
        AccountNo, Amount, CreatedDate
    from TransactionsTest
), cte2 as (
    select
        c.AccountNo, c.CreatedDate, c.Amount, 0 as Ord
    from cte as c
    where rn <= 12
    union all
    select
        c.AccountNo, null as CreatedDate, sum(c.Amount) as Amount, 1 as Ord
    from cte as c
    where rn > 12
    group by c.AccountNo
)
select
    AccountNo, CreatedDate, Amount
from cte2
order by AccountNo, Ord, CreatedDate

<强> sql fiddle demo