1

想知道您是否可以帮助我查询?

在金融部门工作,我的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 笔交易 + 每个账户必须返回过去 12 个月之前所有交易的“StartingBalance”StartingBalance=Sum(Amount)。

有什么建议我该怎么做?

非常感谢

        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;
4

1 回答 1

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

于 2013-09-12T06:30:49.900 回答