MS SQL中的行轉列

create table tb
(
    LocTion 
nvarchar(20),
    
work nvarchar(40),
    
money int,
    time 
int,
    age 
int
)

insert tb select '广州',                   '程序员'      , 1000 ,          5          ,   22 
insert tb select '广州' ,                  '教师'       ,    1700,           10      ,     22 
insert tb select '广州'  ,                 '警察'      ,     1300 ,          15     ,        22 
insert tb select '广州'   ,                '警察'     ,      800   ,          5    ,       22 
insert tb select '上海'    ,               '程序员'  ,     1600     ,      5      ,       21 
insert tb select '上海'     ,              '司机'   ,        1200    ,       15  ,         21 
insert tb select '北京'      ,             '程序员',       1400       ,    5    ,         29

declare @sql nvarchar(4000)
set @sql=''
select @sql=@sql+',sum(case when time='+cast(time as varchar)+' then money else 0 end) as ['+cast(time as varchar)+']'
from (select distinct time from tb) a

exec ('select LocTion,Work'+@sql+',max(age) as age from tb group by LocTion,Work order by LocTion,Work')

drop table tb

/*
LocTion              Work            5           10          15          age         
-------------------- ---------------- ----------- ----------- ----------- ----------- 
北京                   程序员              1400        0           0           29
广州                   程序员              1000        0           0           22
广州                   教师                0           1700        0           22
广州                   警察                800         0           1300        22
上海                   程序员              1600        0           0           21
上海                   司机                0           0           1200        21

*/

Code

create table t
(
    LocTion 
nvarchar(20),
    
work nvarchar(40),
    
money int,
    time 
int,
    age 
int
)

insert t select '广州',                   '程序员'      , 1000 ,          5          ,   22 
insert t select '广州' ,                  '教师'       ,    1700,           10      ,     22 
insert t select '广州'  ,                 '警察'      ,     1300 ,          15     ,        22 
insert t select '广州'   ,                '警察'     ,      800   ,          5    ,       22 
insert t select '上海'    ,               '程序员'  ,     1600     ,      5      ,       21 
insert t select '上海'     ,              '司机'   ,        1200    ,       15  ,         21 
insert t select '北京'      ,             '程序员',       1400       ,    5    ,         29

go


declare @s nvarchar(4000)
set @s='select LocTion,work'
select @s=@s+','+quotename(time)+'=sum(case when time='+rtrim(time) +' then money else 0 end)'
from t group by time

exec (@s+',age from t group by LocTion,work,age')
--生成的语句:
select LocTion,work,[5]=sum(case when time=5 then money else 0 end),[10]=sum(case when time=10 then money else 0 end),[15]=sum(case when time=15 then money else 0 end),age from t group by LocTion,work,age
LocTion              
work                                     5           10          15          age         
-------------------- ---------------------------------------- ----------- ----------- ----------- ----------- 
北京                   程序员                                      1400        0           0           29
广州                   程序员                                      
1000        0           0           22
广州                   教师                                       
0           1700        0           22
广州                   警察                                       
800         0           1300        22
上海                   程序员                                      
1600        0           0           21
上海                   司机                                       
0           0           1200        21

create table t
(
    LocTion 
nvarchar(20),
    
work nvarchar(40),
    
money int,
    time 
int,
    age 
int
)

insert t select '广州',                   '程序员'      , 1000 ,          5          ,   22 
insert t select '广州' ,                  '教师'       ,    1700,           10      ,     22 
insert t select '广州'  ,                 '警察'      ,     1300 ,          15     ,        22 
insert t select '广州'   ,                '警察'     ,      800   ,          5    ,       22 
insert t select '上海'    ,               '程序员'  ,     1600     ,      5      ,       21 
insert t select '上海'     ,              '司机'   ,        1200    ,       15  ,         21 
insert t select '北京'      ,             '程序员',       1400       ,    5    ,         29

go

select 
    LocTion,
    
work,
    
[5]=sum(case when time=5 then money else 0 end),
    
[10]=sum(case when time=10 then money else 0 end),
    
[15]=sum(case when time=15 then money else 0 end),
    age ,
    row
=identity(int,1,1
    
into ##--生成临时表
from 
    t 
group by LocTion,work,age  order by age%2 asc, age asc


select 
    
[LocTion]=case when row=(select min(row) from ## where [LocTion]=a.[LocTion]then [LocTion] else ''end,
    
[work],[5],[10],[15],age
from 
    ## a

--drop table ##



LocTion              
work                                     5           10          15          age         
-------------------- ---------------------------------------- ----------- ----------- ----------- ----------- 
广州                   程序员                                      1000        0           0           22
                     教师                                       
0           1700        0           22
                     警察                                       
800         0           1300        22
上海                   程序员                                      
1600        0           0           21
                     司机                                       
0           0           1200        21
北京                   程序员                                      
1400        0           0           29

(所影响的行数为 
6 行)

posted @ 2008-09-09 19:05  Athrun  阅读(907)  评论(0编辑  收藏  举报