LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

SQL 数据增加小计及合计行,求sql行合计列合计

Ccoffee
2015年12月24日 10:10 本文热度 6588
表  
id 编号
time 时间
bm 部门
fy 费用
数据
id time bm fy
1 2010-1-2 市场部 200
2 2010-1-5 行政部 500
4 2010-1-5 市场部 500
3 2010-3-5 行政部 400
5 2010-1-5 行政部 400
现在我要统计出结果
月份 市场部 行政部 合计
1月份 700 900 1600


。  
12月份 500 400 900
合计 1200 1300 2500
------回答---------
------回答(20分)---------
SQL code
if not object_id(''tb'') is null drop table tb Go Create table tb([id] int,[time] Datetime,[bm] nvarchar(3),[fy] int) Insert tb select 1,''2010-1-2'',N''市场部'',200 union all select 2,''2010-1-5'',N''行政部'',500 union all select 4,''2010-1-5'',N''市场部'',500 union all select 3,''2010-3-5'',N''行政部'',400 union all select 5,''2010-1-5'',N''行政部'',400 Go select ltrim(month(time))+''月份''月份, sum(case when bm=''市场部'' then fy else 0 end)市场部, sum(case when bm=''行政部'' then fy else 0 end)行政部, sum(fy)合计 from tb group by ltrim(month(time))+''月份'' union all select N''合计'', sum(case when bm=''市场部'' then fy else 0 end), sum(case when bm=''行政部'' then fy else 0 end), sum(fy) from tb /* 月份 市场部 行政部 合计 ---------------- ----------- ----------- ----------- 1月份 0 900 1600 3月份 0 400 400 合计 0 1300 2000 */
------回答(20分)---------
漏个合计
SQL code
--> 测试数据: #tb if object_id(''tempdb.dbo.#tb'') is not null drop table #tb go create table #tb (id int,time datetime,bm varchar(6),fy int) insert into #tb select 1,''2010-1-2'',''市场部'',200 union all select 2,''2010-1-5'',''行政部'',500 union all select 4,''2010-1-5'',''市场部'',500 union all select 3,''2010-3-5'',''行政部'',400 union all select 5,''2010-1-5'',''行政部'',400 select [月份]=isnull(ltrim(number)+''月份'',''合计''), [行政部]=sum(case when bm=''行政部'' then fy else 0 end), [市场部]=sum(case when bm=''市场部'' then fy else 0 end), [合计]=sum(isnull(fy,0)) from #tb a right join master..spt_values s on month(time)=number where type=''p'' and number between 1 and 12 group by ltrim(number)+''月份'' with rollup order by cast(replace(replace(isnull(ltrim(number)+''月份'',''合计''),''合计'',''13月份''),''月份'','''') as int) 月份 行政部 市场部 合计 ---------------- ----------- ----------- ----------- 1月份 900 700 1600 2月份 0 0 0 3月份 400 0 400 4月份 0 0 0 5月份 0 0 0 6月份 0 0 0 7月份 0 0 0 8月份 0 0 0 9月份 0 0 0 10月份 0 0 0 11月份 0 0 0 12月份 0 0 0 合计 1300 700 2000 (13 行受影响)

------回答---------

------回答(20分)---------

SQL code
if not object_id(''tb'') is null drop table tb Go Create table tb([id] int,[time] Datetime,[bm] nvarchar(3),[fy] int) Insert tb select 1,''2010-1-2'',N''市场部'',200 union all select 2,''2010-1-5'',N''行政部'',500 union all select 4,''2010-1-5'',N''市场部'',500 union all select 3,''2010-3-5'',N''行政部'',400 union all select 5,''2010-1-5'',N''行政部'',400 Go select ltrim(month(time))+''月份''月份, sum(case when bm=''市场部'' then fy else 0 end)市场部, sum(case when bm=''行政部'' then fy else 0 end)行政部, sum(fy)合计 from tb group by ltrim(month(time))+''月份'' union all select N''合计'', sum(case when bm=''市场部'' then fy else 0 end), sum(case when bm=''行政部'' then fy else 0 end), sum(fy) from tb /* 月份 市场部 行政部 合计 ---------------- ----------- ----------- ----------- 1月份 0 900 1600 3月份 0 400 400 合计 0 1300 2000 */
------回答(20分)---------


漏个合计
SQL code
--> 测试数据: #tb if object_id(''tempdb.dbo.#tb'') is not null drop table #tb go create table #tb (id int,time datetime,bm varchar(6),fy int) insert into #tb select 1,''2010-1-2'',''市场部'',200 union all select 2,''2010-1-5'',''行政部'',500 union all select 4,''2010-1-5'',''市场部'',500 union all select 3,''2010-3-5'',''行政部'',400 union all select 5,''2010-1-5'',''行政部'',400 select [月份]=isnull(ltrim(number)+''月份'',''合计''), [行政部]=sum(case when bm=''行政部'' then fy else 0 end), [市场部]=sum(case when bm=''市场部'' then fy else 0 end), [合计]=sum(isnull(fy,0)) from #tb a right join master..spt_values s on month(time)=number where type=''p'' and number between 1 and 12 group by ltrim(number)+''月份'' with rollup order by cast(replace(replace(isnull(ltrim(number)+''月份'',''合计''),''合计'',''13月份''),''月份'','''') as int) 月份 行政部 市场部 合计 ---------------- ----------- ----------- ----------- 1月份 900 700 1600 2月份 0 0 0 3月份 400 0 400 4月份 0 0 0 5月份 0 0 0 6月份 0 0 0 7月份 0 0 0 8月份 0 0 0 9月份 0 0 0 10月份 0 0 0 11月份 0 0 0 12月份 0 0 0 合计 1300 700 2000 (13 行受影响)

该文章在 2015/12/24 10:12:04 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved