在 SQL 服务器中使用汇总计算小计
Calculating Subtotals using rollup in SQL Server
我有一个 table(#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'
感谢任何帮助。
谢谢
我不确定你为什么要为此使用 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
您似乎只是在分组中汇总部门,因此您只需使用 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
我有一个 table(#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'
感谢任何帮助。
谢谢
我不确定你为什么要为此使用 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
您似乎只是在分组中汇总部门,因此您只需使用 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