简体   繁体   English

在 SQL Server 2005 中添加总行?

[英]Adding a total row in SQL Server 2005?

I have a script that creates a report however I require a total row at the bottom.我有一个创建报告的脚本,但是我需要底部的总行。

In SQL Server 2008 I think I would use roll up but I cannot work out how to do this 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

The aim is to add total to bottom of the table for all the columns about from StoreID and Store Name目的是将来自 StoreID 和 Store Name 的所有列的总数添加到表的底部

Any ideas?有任何想法吗?

I would expect you can add a UNION ALL which selects a row containing the aggregates without any of the grouping, which should give you a figure for all the rows:我希望您可以添加一个UNION ALL来选择包含没有任何分组的聚合的行,这应该为您提供所有行的数字:

 -- 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

It may not be quite as simple as that, given that in the main query you've got some extra group by fields which don't actually appear directly in the select, but that's the general idea.考虑到在主查询中您有一些额外的 group by 字段,这些字段实际上并未直接出现在选择中,因此它可能并不那么简单,但这是总体思路。 Without seeing the schema, sample data and expected output, it's hard to be more precise (because it's impossible to test the query).如果没有看到架构、示例数据和预期输出,就很难更精确(因为不可能测试查询)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM