如何为每个项目创建一个具有总计的新列?

时间:2019-02-27 14:56:57

标签: sql-server tsql

我正在使用Sql Server 2014,并且有一个T-SQL查询,其内容如下:

    Use MyDatabase
     Select Market, Sum([RN]) as [RN]
     From [View1]
     Group by Market

输出如下:

          Market               RN
          UK                      100
          FRANCE             125
          GERMANY          300

我需要在输出中添加新列,如下所示:

     Market               RN.         Grand Total
     UK                      100        525
     FRANCE             125        525
     GERMANY          300       525

我该怎么做?

1 个答案:

答案 0 :(得分:3)

您想要窗口功能:

select distinct market, 
       sum(rn) over (partition by market) as rn, 
       sum(rn) over () as Total
from view1 v1;