SQL 之Group by with Rollup 的效果/*
建立示例过程,以演示Group by with Rollup 的效果 */ -- 建一临时表 create table #tmpA ( Dept char(3) , Sect char(3) , line char(3) , Line_Desc varchar(30) , Title_code char(3) , Title_Desc varchar(30) , Headcount int ) -- 加入一些数据记录 Insert #tmpA Select ’DA’ , ’S1’ , ’La’ , ’Line a ’ , ’T1’ , ’Title Desc 1’, 3 Insert #tmpA Select ’DA’ , ’S1’ , ’La’ , ’Line a ’ , ’T2’ , ’Title Desc 2’ , 3 Insert #tmpA Select ’DA’ , ’S1’ , ’Lb’ , ’Line b ’ , ’T1’ , ’Title Desc 1’ , 3 Insert #tmpA Select ’DA’ , ’S1’ , ’Lb’ , ’Line b ’ , ’T2’ , ’Title Desc 2’ , 3 Insert #tmpA Select ’DA’ , ’S2’ , ’La’ , ’Line a ’ , ’T1’ , ’Title Desc 1’ , 3 Insert #tmpA Select ’DA’ , ’S2’ , ’Lb’ , ’Line b ’ , ’T1’ , ’Title Desc 1’ , 3 Insert #tmpA Select ’DB’ , ’S1’ , ’Lb’ , ’Line b ’ , ’T1’ , ’Title Desc 1’ , 3 Insert #tmpA Select ’DB’ , ’S2’ , ’La’ , ’Line a ’ , ’T1’ , ’Title Desc 1’ , 3 Insert #tmpA Select ’DB’ , ’S2’ , ’Lb’ , ’Line b ’ , ’T1’ , ’Title Desc 1’ , 3 Insert #tmpA Select ’DC’ , ’S1’ , ’La’ , ’Line a ’ , ’T1’ , ’Title Desc 1’ , 3 Insert #tmpA Select ’DC’ , ’S2’ , ’Lb’ , ’Line b ’ , ’T1’ , ’Title Desc 1’ , 3 select * from #tmpA -- 直接使用的效果 select dept , sect , line , title_code , max(title_desc) as title_desc , sum(headcount) as Headcounts from #tmpA group by dept , sect , line , title_code with rollup -- 对其中一个进行转换标题的效果 select dept , sect , line , ( case when title_code is null then ’total ( by Line ) : ’ else title_code end ) as title_code, sum(headcount) as Headcounts from #tmpA group by dept , sect , line , title_code with rollup
|
|
||||
|
|
||||
|
|
|
||||
|
|
||||
|
|
|
||||
|
|
||||
|
|