国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

模擬多級表頭的分組統(tǒng)計

2024-07-21 02:11:24
字體:
來源:轉載
供稿:網(wǎng)友

原帖地址:
http://community.csdn.net/expert/topic/3434/3434688.xml?temp=3.246486e-03

--測試數(shù)據(jù)
create table 單位表 (單位代碼 varchar(10),單位名稱 varchar(50))
insert 單位表 values('01'      ,'中國單位')             --1級
insert 單位表 values('0101'    ,'山東單位')             --2級
insert 單位表 values('010101'  ,'山東濟南單位')          --3級
insert 單位表 values('010102'  ,'山東青島單位')          --3級
insert 單位表 values('01010201','山東青島即默單位一')     --4級
insert 單位表 values('01010202','山東青島即默單位二')     --4級
insert 單位表 values('0102'    ,'山西單位')              --2級
insert 單位表 values('010201'  ,'山西大同單位')          --3級
insert 單位表 values('0103'    ,'陜西單位')              --2級
insert 單位表 values('010301'  ,'陜西西安單位')          --3級
--insert 單位表 values('01030101'  ,'陜西西安a單位')          --3級
--insert 單位表 values('0103010101'  ,'陜西西安aa單位')          --3級
insert 單位表 values('010302'  ,'陜西咸陽單位')          --3級

create table 供應表 (物資編號 varchar(10),物資名稱 varchar(50),單位代碼 varchar(10),供應數(shù)量 int)
insert 供應表 values('0001','電子稱','010101',1)
insert 供應表 values('0002','電動機','010101',1)
insert 供應表 values('0001','電子稱','01010201',1)
insert 供應表 values('0002','電動機','01010201',1)
insert 供應表 values('0001','電子稱','010201',1)
insert 供應表 values('0003','電動刷','010201',1)
insert 供應表 values('0004','電動車','010302',1)
go

/*--要求
 
 分級匯總,然后轉置得到如下結果:
select '','','','中國單位','中國單位','中國單位','中國單位','中國單位','中國單位','中國單位','中國單位','中國單位'
union all select '','','','山東單位','山東單位','山東單位','山東單位','山西單位','山西單位','陜西單位','陜西單位','陜西單位'
union all select '','','',' ',' ','山東青島單位','山東青島單位',' ',' ',' ',' ',' '
union all select '序號','裝備名稱','合計','小計','山東濟南單位','山東青島即默單位一','山東青島即默單位二','小計','山西大同單位','小計','陜西西安單位','陜西咸陽單位'
union all select '','總計','7','4','2','2','','2','2','1','','1'
union all select '1','(0001)電子稱','3','2','1','1','','1','1','','',''
union all select '2','(0002)電動機','2','2','1','1','','','','','','' 
union all select '3','(0003)電動刷','1','','','','','1','1','','',''   
union all select '4','(0004)電動車','1','','','','','','','1','','1'


統(tǒng)計結果說明:
1.單位代碼固定每兩位為1級
2.統(tǒng)計單位表中所有的最末級,如果該單位在供應表中無數(shù)據(jù),則對應顯示為0
3.小計是根據(jù)二級單位合并得到
4.結果中的表頭是分級的,一級單位在第一行,二級單位在第二行,如果該單位已經(jīng)在統(tǒng)計數(shù)據(jù),即"序號','裝備名稱','合計'"這行出現(xiàn),則不再在對應的級數(shù)的表頭里面出現(xiàn)
--*/

--查詢處理
declare @i varchar(10),@s11 varchar(8000),@s12 varchar(8000),@s13 varchar(8000)
 ,@s2 varchar(8000),@s3 varchar(8000),@s14 varchar(8000)
 
select @s11='',@s12='',@s13='',@s14=''
 ,@s2='',@s3=''

select a=left(單位代碼,4),b=left(單位代碼,len(單位代碼)-2),c=len(單位代碼)-2
into # from 單位表 b
where not exists(select * from 單位表 where 單位代碼 like b.單位代碼+'__')
order by 單位代碼

select @i=max(len(b)) from #
while @i>'0'
 select @s11=',@'[email protected]+' varchar(8000)'[email protected]
  ,@s12=',@'[email protected]+'='''''''''','''''''','''''''''''[email protected]
  ,@s13='
set @=null select @'[email protected]+'[email protected]'[email protected]+'+case when @=a then '''' else '
   +case when @i>'4' then ''','''''''''' end' else ''',''''''+單位名稱+'''''''' end' end
   +'+'',''''''+單位名稱+'''''''',@=a from '
   +case when @i<='4' then '# a,單位表 b where left(a.b,'[email protected]+')=b.單位代碼'
    else '(select 單位名稱=case when a.c>='[email protected]+' then 單位名稱 else '''' end,a.* from # a,單位表 b where left(a.b,'[email protected]+')=b.單位代碼)a' end
   [email protected]
  ,@s14='+'' union all select ''[email protected]'[email protected][email protected]
  ,@[email protected]
select @s12=stuff(@s12,1,1,'')
 ,@s14=stuff(@s14,1,13,'')

select @[email protected]+','''+case when len(b.單位代碼)=4 then '小計' else b.單位名稱 end+''''
 ,@[email protected]+case
  when len(b.單位代碼)=4
  then ',['+b.單位名稱+'_小計]=cast(sum(case left(單位代碼,4) when '''+b.單位代碼+''' then 供應數(shù)量 else 0 end) as varchar)'
  else ',['+b.單位名稱+']=cast(sum(case 單位代碼 when '''+b.單位代碼+''' then 供應數(shù)量 else 0 end) as varchar)'
  end
from 單位表 b
where len(單位代碼)=4
 or not exists(select * from 單位表 where 單位代碼 like b.單位代碼+'__')
order by 單位代碼
set @s2=replace(@s2,'''','''''')

exec('
select 序號=cast('''' as varchar(10))
 ,裝備名稱=case
  when grouping(物資編號)=1 then ''總計''
  else  ''(''+物資編號+'')''+物資名稱 end
 ,供應數(shù)量=cast(sum(供應數(shù)量) as varchar)'[email protected]+'
into #t
from 供應表
group by 物資編號,物資名稱 with rollup
having grouping(物資名稱)=0 or grouping(物資編號)=1
order by grouping(物資編號) desc
declare @i int
set @i=-1
update #t set @[email protected]+1,序號=case when @i=0 then '''' else cast(@i as varchar) end

declare @ varchar(10)'[email protected]+'
select '[email protected]+'
'[email protected]+'
exec('''[email protected]+'+''
union all
select ''''序號'''',''''裝備名稱'''',''''合計'''''[email protected]+'
union all
select * from #t
'')
drop table #
')
go

--刪除測試
drop table 單位表,供應表

/*--測試結果(自己看)--*/

 
  • 本文來源于網(wǎng)頁設計愛好者web開發(fā)社區(qū)http://www.html.org.cn收集整理,歡迎訪問。
  • 發(fā)表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發(fā)表
    主站蜘蛛池模板: 阳春市| 临江市| 浪卡子县| 德钦县| 宁夏| 峨边| 萍乡市| 广安市| 新巴尔虎右旗| 泽州县| 黄梅县| 江山市| 连南| 临桂县| 晋中市| 沽源县| 兴化市| 永登县| 西乌珠穆沁旗| 洛浦县| 昔阳县| 张掖市| 正安县| 临沂市| 特克斯县| 张家川| 阿图什市| 交城县| 雷州市| 罗田县| 彭山县| 平定县| 赤壁市| 永州市| 喜德县| 临沂市| 定日县| 舒兰市| 广宁县| 山阴县| 巴中市|