T-SQL-每个项目的运行总计

时间:2018-11-28 12:21:01

标签: tsql sql-server-2017

我有一个简化的表结构:

enter image description here

我需要生成一个汇总结果,将每个项目/期间的交易总和-每个项目的运行总额。我添加了TransactionCountInPeriod列

如何编写查询以获取如下结果:

enter image description here

就查询而言,这是我一直无法解决的问题。我无法弄清楚如何每个ProjectId

<xf:itemset ref="$form-resources/CONTENT_OF_MY-TEXT_HERE/item">
    <xf:label ref="label"/>
    <xf:value ref="value"/>
</xf:itemset>

架构和测试数据

SELECT 
    prj.Id AS ProjectId,
    p.Id AS PeriodId,
    SUM(t.Amount) AS SumInPeriod--,
    --SUM(t.Amount) OVER (PARTITION BY prj.Id) AS RunningTotal
    --COUNT(t.*) AS TransactionCountInPeriod
FROM 
    Periods p
    CROSS JOIN Projects prj
    LEFT OUTER JOIN Transactions t ON p.Id = t.PeriodId
GROUP BY 
    prj.Id,
    p.Id
ORDER BY
    prj.Id,
    p.Id

(我会创建一个sql小提琴,但是当我尝试时它总是崩溃...)

2 个答案:

答案 0 :(得分:3)

您可以使用SUM窗口函数来计算运行总计。

DECLARE @Periods TABLE(
    [Id] [int] NOT NULL
)

DECLARE @Projects TABLE(
    [Id] [int] NOT NULL,
    [Name] [varchar](50) NOT NULL
)

DECLARE @Transactions TABLE(
    [Id] [int] NOT NULL,
    [ProjectId] [int] NOT NULL,
    [PeriodId] [int] NOT NULL,
    [Amount] [decimal](18, 0) NOT NULL
)

INSERT @Periods ([Id]) VALUES (201801)
INSERT @Periods ([Id]) VALUES (201802)
INSERT @Periods ([Id]) VALUES (201803)
INSERT @Periods ([Id]) VALUES (201804)
INSERT @Periods ([Id]) VALUES (201805)
INSERT @Projects ([Id], [Name]) VALUES (1, N'Alpha')
INSERT @Projects ([Id], [Name]) VALUES (2, N'Beta')
INSERT @Transactions ([Id], [ProjectId], [PeriodId], [Amount]) VALUES (1, 1, 201801, CAST(100 AS Decimal(18, 0)))
INSERT @Transactions ([Id], [ProjectId], [PeriodId], [Amount]) VALUES (2, 1, 201801, CAST(100 AS Decimal(18, 0)))
INSERT @Transactions ([Id], [ProjectId], [PeriodId], [Amount]) VALUES (3, 1, 201802, CAST(100 AS Decimal(18, 0)))
INSERT @Transactions ([Id], [ProjectId], [PeriodId], [Amount]) VALUES (4, 1, 201803, CAST(100 AS Decimal(18, 0)))
INSERT @Transactions ([Id], [ProjectId], [PeriodId], [Amount]) VALUES (5, 1, 201803, CAST(100 AS Decimal(18, 0)))
INSERT @Transactions ([Id], [ProjectId], [PeriodId], [Amount]) VALUES (6, 1, 201804, CAST(100 AS Decimal(18, 0)))
INSERT @Transactions ([Id], [ProjectId], [PeriodId], [Amount]) VALUES (7, 2, 201801, CAST(100 AS Decimal(18, 0)))
INSERT @Transactions ([Id], [ProjectId], [PeriodId], [Amount]) VALUES (8, 2, 201801, CAST(100 AS Decimal(18, 0)))
INSERT @Transactions ([Id], [ProjectId], [PeriodId], [Amount]) VALUES (12, 2, 201804, CAST(100 AS Decimal(18, 0)))

select
    pr.Id as ProjectId
    , pe.Id as PeriodId
    , isnull(tr.SumAmount, 0) as SumInPeriod
    , isnull(sum(tr.SumAmount) over(partition by pr.Id order by pe.Id), 0) as RunningTotal
    , tr.TransactionsCount as TransactionCountInPeriod
from @Projects pr
cross join @Periods pe
outer apply (select sum(t.Amount) as SumAmount, count(*) as TransactionsCount from @Transactions t where t.ProjectId = pr.Id and t.PeriodId = pe.Id)  tr
order by ProjectId, PeriodId

答案 1 :(得分:2)

最简单的选择是使用sum...over
您的位置非常接近-但您不需要分组依据,并且确实需要在order by子句中添加over来获得运行总计-像这样:

SELECT  pr.Id As ProjectId,
        pe.Id As PeriodId,
        SUM(Amount) OVER(PARTITION BY pe.Id, pr.Id) AS SumInPeriod,
        SUM(Amount) OVER(PARTITION BY pr.Id ORDER BY pe.Id) AS RunningTotal,
        COUNT(Amount) OVER(PARTITION BY pe.Id, pr.Id) TransactionCountInPeriod
FROM (
    Periods pe
    CROSS JOIN projects as pr
    )
LEFT JOIN Transactions tr 
    ON pe.Id = tr.PeriodId
    AND tr.ProjectId = pr.Id
ORDER BY pr.id, pe.id