我有以下查询,该查询返回,商店编号及其区域以及2016年商店销售额的总和。
SELECT EU.[STORE NO] ,EU.REGION ,SUM(SA.SALESEXVAT) AS 'STORE SALES' FROM [BHXSQL2014-DEV].BManalytics.dbo.EUactivestores EU INNER JOIN EUUKSQL01.dashboard.dbo.stocksalesaggregateweek sa ON eu.[Store No] = sa.[branchno] WHERE sa.Fiscalyear = 2016 GROUP BY EU.[Store No] ,EU.REGION
我将如何添加第四列,以总结每个商店所在区域的销售额。
每个区域有许多存储,因此,在相同区域中具有存储的每个ROW上,该值将相同。
希望我能解释清楚吗?
您可以使用窗口功能:
SELECT EU.[STORE NO], EU.REGION, SUM(SA.SALESEXVAT) AS STORE_SALES SUM(SUM(SA.SALESEXVAT)) OVER (PARTITION BY EU.REGION) as REGION_STORE_SALES FROM [BHXSQL2014-DEV].BManalytics.dbo.EUactivestores EU INNER JOIN EUUKSQL01.dashboard.dbo.stocksalesaggregateweek sa ON eu.[Store No] = sa.[branchno] WHERE sa.Fiscalyear = 2016 GROUP BY EU.[Store No], EU.REGION ;