在 SQL Server 2005 中添加总行?
Adding a total row in SQL Server 2005?
我有一个创建报告的脚本,但是我需要在底部有一个总行。
在 SQL Server 2008 中,我想我会使用汇总,但我不知道如何在 SQL Server 2005 中执行此操作。
USE FEN
GO
SELECT
PMC.Store_ID,
PMC.Name,
SUM (PMC.[Net Sales] / 1000) AS 'Daily Sales',
SUM (PMCLY.[Net Sale LY] / 1000) AS 'Daily Sales LY',
CAST(100.0*(PMC.[Net Sales] - PMCLY.[Net Sale LY]) / PMCLY.[Net Sale LY] AS Decimal (10,2)) AS 'VAR % Vs LY',
SUM (PMCW.[Net Sales Week] / 1000) AS 'Daily Sales Week',
SUM (PMCLYW.[Net Sale Week LY] /1000) AS 'Daily Sales Week LY',
CAST(100.0*(PMCW.[Net Sales Week] - PMCLYW.[Net Sale Week LY]) / PMCLYW.[Net Sale Week LY] AS Decimal (10,2)) AS 'VAR % Vs LY'
FROM PMC_DailySalesReport AS pmc
JOIN PMC_DailySalesReportLY as pmcly on pmcly.Store_ID = pmc.Store_ID
JOIN PMC_DailySalesReportWeek as PMCW on pmcw.Store_ID = pmc.Store_ID
JOIN PMC_DailySalesReportLYWeek AS PMCLYW on PMCLYW.Store_ID = PMC.Store_ID
GROUP BY
PMC.Store_ID,
PMC.Name,
PMC.[Net Sales],
PMCLY.[Net Sale LY],
PMCLYW.[Net Sale Week LY],
PMCW.[Net Sales Week]
go
目的是将关于 StoreID 和 Store Name
的所有列的总计添加到 table 的底部
有什么想法吗?
我希望您可以添加一个 UNION ALL
,其中 select 是包含没有任何分组的聚合的行,这应该为您提供所有行的数字:
-- your query above, and then:
UNION ALL
SELECT
'All' As Store_ID,
'All' As Name,
SUM (PMC.[Net Sales] / 1000) AS 'Daily Sales',
SUM (PMCLY.[Net Sale LY] / 1000) AS 'Daily Sales LY',
CAST(100.0*(PMC.[Net Sales] - PMCLY.[Net Sale LY]) / PMCLY.[Net Sale LY] AS Decimal (10,2)) AS 'VAR % Vs LY',
SUM (PMCW.[Net Sales Week] / 1000) AS 'Daily Sales Week',
SUM (PMCLYW.[Net Sale Week LY] /1000) AS 'Daily Sales Week LY',
CAST(100.0*(PMCW.[Net Sales Week] - PMCLYW.[Net Sale Week LY]) / PMCLYW.[Net Sale Week LY] AS Decimal (10,2)) AS 'VAR % Vs LY'
FROM PMC_DailySalesReport AS pmc
JOIN PMC_DailySalesReportLY as pmcly on pmcly.Store_ID = pmc.Store_ID
JOIN PMC_DailySalesReportWeek as PMCW on pmcw.Store_ID = pmc.Store_ID
JOIN PMC_DailySalesReportLYWeek AS PMCLYW on PMCLYW.Store_ID = PMC.Store_ID
它可能没有那么简单,因为在主查询中你有一些额外的分组依据字段,这些字段实际上并没有直接出现在 select 中,但这是一般的想法.没有看到架构、示例数据和预期输出,很难更精确(因为无法测试查询)。
我有一个创建报告的脚本,但是我需要在底部有一个总行。
在 SQL Server 2008 中,我想我会使用汇总,但我不知道如何在 SQL Server 2005 中执行此操作。
USE FEN
GO
SELECT
PMC.Store_ID,
PMC.Name,
SUM (PMC.[Net Sales] / 1000) AS 'Daily Sales',
SUM (PMCLY.[Net Sale LY] / 1000) AS 'Daily Sales LY',
CAST(100.0*(PMC.[Net Sales] - PMCLY.[Net Sale LY]) / PMCLY.[Net Sale LY] AS Decimal (10,2)) AS 'VAR % Vs LY',
SUM (PMCW.[Net Sales Week] / 1000) AS 'Daily Sales Week',
SUM (PMCLYW.[Net Sale Week LY] /1000) AS 'Daily Sales Week LY',
CAST(100.0*(PMCW.[Net Sales Week] - PMCLYW.[Net Sale Week LY]) / PMCLYW.[Net Sale Week LY] AS Decimal (10,2)) AS 'VAR % Vs LY'
FROM PMC_DailySalesReport AS pmc
JOIN PMC_DailySalesReportLY as pmcly on pmcly.Store_ID = pmc.Store_ID
JOIN PMC_DailySalesReportWeek as PMCW on pmcw.Store_ID = pmc.Store_ID
JOIN PMC_DailySalesReportLYWeek AS PMCLYW on PMCLYW.Store_ID = PMC.Store_ID
GROUP BY
PMC.Store_ID,
PMC.Name,
PMC.[Net Sales],
PMCLY.[Net Sale LY],
PMCLYW.[Net Sale Week LY],
PMCW.[Net Sales Week]
go
目的是将关于 StoreID 和 Store Name
的所有列的总计添加到 table 的底部有什么想法吗?
我希望您可以添加一个 UNION ALL
,其中 select 是包含没有任何分组的聚合的行,这应该为您提供所有行的数字:
-- your query above, and then:
UNION ALL
SELECT
'All' As Store_ID,
'All' As Name,
SUM (PMC.[Net Sales] / 1000) AS 'Daily Sales',
SUM (PMCLY.[Net Sale LY] / 1000) AS 'Daily Sales LY',
CAST(100.0*(PMC.[Net Sales] - PMCLY.[Net Sale LY]) / PMCLY.[Net Sale LY] AS Decimal (10,2)) AS 'VAR % Vs LY',
SUM (PMCW.[Net Sales Week] / 1000) AS 'Daily Sales Week',
SUM (PMCLYW.[Net Sale Week LY] /1000) AS 'Daily Sales Week LY',
CAST(100.0*(PMCW.[Net Sales Week] - PMCLYW.[Net Sale Week LY]) / PMCLYW.[Net Sale Week LY] AS Decimal (10,2)) AS 'VAR % Vs LY'
FROM PMC_DailySalesReport AS pmc
JOIN PMC_DailySalesReportLY as pmcly on pmcly.Store_ID = pmc.Store_ID
JOIN PMC_DailySalesReportWeek as PMCW on pmcw.Store_ID = pmc.Store_ID
JOIN PMC_DailySalesReportLYWeek AS PMCLYW on PMCLYW.Store_ID = PMC.Store_ID
它可能没有那么简单,因为在主查询中你有一些额外的分组依据字段,这些字段实际上并没有直接出现在 select 中,但这是一般的想法.没有看到架构、示例数据和预期输出,很难更精确(因为无法测试查询)。