在SQL Server中使用汇总计算小计

时间:2016-01-14 20:56:25

标签: sql-server rollup

我有一张表(#mytable),其中包含有关公司的基本财务信息。

CREATE TABLE #mytable 
(
        Companyid varchar2(50),
        DataDescription varchar2(100),
        Value DECIMAL(23,6),
        Department varchar2(100),
        CurrencyIS03 varchar2(5),
        DateofData datetime
)

INSERT INTO #mytable (Companyid, DataDescription, Value, Department, CurrencyIS03, DateofData)
    SELECT 
        'A100', 'Revenue', '1000.00', 'Corporate', 'USD', '2014-12-31 00:00:00' 
    UNION ALL
    SELECT 'A100','Revenue','2000.00','Banking','USD','2014-12-31 00:00:00'     
    UNION ALL
    SELECT 'A100','Revenue','2500.00','Corporate','USD','2013-12-31 00:00:00' 
    UNION ALL
    SELECT 'A100','Revenue','3000.00','Banking','USD','2013-12-31 00:00:00' 
    UNION ALL
    SELECT 'A100','Operating Income','10000.00','Corporate','USD','2014-12-31 00:00:00' 
    UNION ALL
    SELECT 'A100','Operating Income','1000.00','Banking','USD','2014-12-31 00:00:00' 
    UNION ALL
    SELECT 'A200','Revenue','1100.00','Corporate','USD','2013-12-31 00:00:00' 
    UNION ALL
    SELECT 'A200','Revenue','3000.00','Banking','USD','2013-12-31 00:00:00' 
    UNION ALL
    SELECT 'A200','Operating Income','5500.00','Corporate','USD','2014-12-31 00:00:00' 
    UNION ALL
    SELECT 'A200','Operating Income','10000.00','Banking','USD','2014-12-31 00:00:00' 

我必须根据Companyid,DataDescription,Department,CurrencyIS03,DateofData查找小计。我不知道该怎么做。我尝试了以下

select 
    Companyid, DataDescription,
    sum(Value) as total,
    Department, CurrencyIS03, DateofData 
from 
    #mytable
group by 
    rollup(CompanyID, Datadescription, Department, CurrencyIS03, DateofData)

这不会返回正确的答案。

以下是我的期望。

CREATE TABLE #outputtable 
(
        Companyid varchar2(50),
        DataDescription varchar2(100),
        TotalValue DECIMAL(23,6),
        Department varchar2(100),
        CurrencyIS03 varchar2(5),
        DateofData datetime
)

INSERT INTO #outputtable (Companyid, DataDescription, TotalValue, Department, CurrencyIS03, DateofData)
 SELECT 'A100','Revenue','1000.00','Corporate','USD','2014-12-31 00:00:00' UNION ALL
 SELECT 'A100','Revenue','2000.00','Banking','USD','2014-12-31 00:00:00' UNION ALL
 SELECT 'A100','Revenue','3000.00','Total','USD','2014-12-31 00:00:00' UNION ALL

 SELECT 'A100','Revenue','2500.00','Corporate','USD','2013-12-31 00:00:00' UNION ALL
 SELECT 'A100','Revenue','3000.00','Banking','USD','2013-12-31 00:00:00' UNION ALL
 SELECT 'A100','Revenue','5500.00','Total','USD','2013-12-31 00:00:00' UNION ALL

 SELECT 'A100','Operating Income','10000.00','Corporate','USD','2014-12-31 00:00:00' UNION ALL
 SELECT 'A100','Operating Income','1000.00','Banking','USD','2014-12-31 00:00:00' UNION ALL
 SELECT 'A100','Operating Income','11000.00','Total','USD','2014-12-31 00:00:00' UNION ALL

 SELECT 'A200','Revenue','1100.00','Corporate','USD','2013-12-31 00:00:00' UNION ALL
 SELECT 'A200','Revenue','3000.00','Banking','USD','2013-12-31 00:00:00' UNION ALL
 SELECT 'A200','Revenue','4100.00','Total','USD','2013-12-31 00:00:00' UNION ALL

 SELECT 'A200','Operating Income','5500.00','Corporate','USD','2014-12-31 00:00:00' UNION ALL
 SELECT 'A200','Operating Income','10000.00','Banking','USD','2014-12-31 00:00:00' UNION ALL
 SELECT 'A200','Operating Income','15500.00','Total','USD','2014-12-31 00:00:00' 

感谢任何帮助。

由于

2 个答案:

答案 0 :(得分:1)

看起来你只是在分组中卷起部门,所以你只需要使用ROLLUP(Department)并正常分组。

SELECT  Companyid,
        DataDescription,
        SUM(Value) AS total,
        COALESCE(Department,'Total') Department,
        CurrencyIS03,
        DateofData
FROM    #mytable
GROUP BY CompanyID,
        Datadescription,
        ROLLUP(Department),
        CurrencyIS03,
        DateofData
ORDER BY CompanyID ASC,
        Datadescription DESC,
        DateofData DESC,
        Department ASC

答案 1 :(得分:0)

我不确定你为什么要使用ROLLUP。

我会这样做:

select * from 
(select Companyid,DataDescription,sum(Value) as Value ,'Total' as Department,CurrencyIS03,DateofData 
 from mytable
 group by CompanyID, Datadescription, CurrencyIS03,DateofData
 union all
 select * from mytable) a
 order by  CompanyID asc, Datadescription desc, DateofData desc, Department asc

有一个fiddle