在SQL Server中获得Total Branch明智

时间:2013-01-10 12:30:17

标签: sql sql-server sql-server-2008

SELECT  b.BranchName ,
        pm.AgreementValue
FROM    dbo.Member AS m
        INNER JOIN dbo.PlanMaster AS pm ON ( m.PlanId = pm.PlanId )
        INNER JOIN dbo.Branch AS b ON ( b.BranchId = m.BranchId )

这是上述查询的结果

BranchName      AgreementValue
------------------------------
abc             60000.00
abc             36000.00
abc             36000.00
xyz             20000.00
xyz             10000.00

现在我希望总共可以获得AgreementValue BranchName ..谢谢你的帮助

2 个答案:

答案 0 :(得分:2)

GROUP BY b.BranchName SUM就像这样:

SELECT   b.BranchName ,
         SUM(pm.AgreementValue) TotalValue
FROM     dbo.Member AS m
         INNER JOIN dbo.PlanMaster AS pm ON ( m.PlanId = pm.PlanId )
         INNER JOIN dbo.Branch AS b ON ( b.BranchId = m.BranchId )
GROUP BY b.BranchName;

答案 1 :(得分:0)

如果您尝试获取每行的总数,请使用sum()的窗口函数:

SELECT  b.BranchName ,
        pm.AgreementValue,
        sum(pm.AgreementValue) over (partition by b.BranchName) as BranchTotal
FROM    dbo.Member AS m
        INNER JOIN dbo.PlanMaster AS pm ON ( m.PlanId = pm.PlanId )
        INNER JOIN dbo.Branch AS b ON ( b.BranchId = m.BranchId )