91网首页-91网页版-91网在线观看-91网站免费观看-91网站永久视频-91网站在线播放

LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發文檔 其他文檔  
 
網站管理員

SQL 數據增加小計及合計行,求sql行合計列合計

Ccoffee
2015年12月24日 10:10 本文熱度 7483
表  
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-2025 ClickSun All Rights Reserved

主站蜘蛛池模板: 九色蝌蚪首页 | www.色黄| 国产区精品自拍 | 97视频日韩 | 成人午夜在线视频 | 日韩欧美大片精品黄 | 国产高清在线视频色 | 午夜在线一区二区 | 欧美日韩国产一区 | 福利片91| 不卡国产视频第一页 | 无码成人1000部免费视频 | 91视频国产精品 | 日韩国产一区二区 | 国产精品第100页 | 精品一区视频 | 国产性色的 | 91福利资源 | 日韩理论中文在 | www.黄在线观看 | 日本最大色倩网站 | 精品乱码一区 | www.99re8| 日本免费一级二级三 | 日本欧美日韩视 | 欧美亚洲日| 看中文欧美性爱大片 | 成人午夜性影院 | 韩国三级国产欧美 | 日韩制服在线 | 日韩精品美女视频 | 精品综合视频 | 午夜不卡影院 | 欧美一级α片在线 | 日本一区二区高清 | 国产在线一品 | 国产亚洲一区二区三 | 破了亲妺妺的处免费 | 国产日韩在线视 | 日韩二三区 | 精品国产午夜肉伦 |