我有这样的记录: ID CITY NAME AGE
1 上海 X 23
2 北京 X 23
3 上海 Y 24
4 深圳 Y 24
5 上海 Z 24
...
很多记录,
我现在又创建了个表2,
ID 上海 北京 深圳 name age
我想把我原来表里的记录都插到表2中,但是要根据实际内容来插,就是变成: ID 上海 北京 深圳 name age
1 上海 北京 NULL X 23
2 上海 NULL 深圳 Y 24
....
怎么办啊?
1 上海 X 23
2 北京 X 23
3 上海 Y 24
4 深圳 Y 24
5 上海 Z 24
...
很多记录,
我现在又创建了个表2,
ID 上海 北京 深圳 name age
我想把我原来表里的记录都插到表2中,但是要根据实际内容来插,就是变成: ID 上海 北京 深圳 name age
1 上海 北京 NULL X 23
2 上海 NULL 深圳 Y 24
....
怎么办啊?
insert # select 1, '上海', 'X', 23
insert # select 2, '北京', 'X', 23
insert # select 3, '上海', 'Y', 24
insert # select 4, '深圳', 'Y', 24
insert # select 5, '上海', 'Z', 24 godeclare @s nvarchar(4000),@s2 nvarchar(4000),@s3 nvarchar(4000)
select @s='',@s2='',@s3=''
select @s= @s+','+quotename(CITY)+'=max(case when CITY='+quotename(CITY,'''')+' then '+quotename(CITY,'''')+' end)',
@s2=@s2+' when CITY='+quotename(CITY,'''')+' then NAME ',
@s3=@s3+' when CITY='+quotename(CITY,'''')+' then AGE '
from
#
group by CITYselect @s2=',[Name]= max(case '+@s2+' end)', @s3=',[AGE]= max(case '+@s3+' end)'exec('select row'+@s+@s2+@s3+'
from
(select * ,row=(select count(1) from # where CITY=t.CITY and ID!>t.ID )
from # t)Tmp
group by row')
row 北京 上海 深圳 Name AGE
----------- ---- ---- ---- ---- -----------
1 北京 上海 深圳 Y 24
2 NULL 上海 NULL Y 24
3 NULL 上海 NULL Z 24
insert # select 1, '上海', 'X', 23
insert # select 2, '北京', 'X', 23
insert # select 3, '上海', 'Y', 24
insert # select 4, '深圳', 'Y', 24
insert # select 5, '上海', 'Z', 24 gocreate table #2(ID int, 上海 nvarchar(5),北京 nvarchar(5),深圳 nvarchar(5), NAME varchar(2) , AGE int)
godeclare @s nvarchar(4000),@s2 nvarchar(4000),@s3 nvarchar(4000)
select @s='',@s2='',@s3=''
select @s=@s+','+quotename(CITY)+'=max(case when CITY='+quotename(CITY,'''')
+' then '+quotename(CITY,'''')+' end)',
@s2=@s2+' when CITY='+quotename(CITY,'''')+' then NAME ',
@s3=@s3+' when CITY='+quotename(CITY,'''')+' then AGE 'from
# t
group by CITY
order by (select min(ID) from # where CITY=t.CITY) asc--排序select @s2=',[Name]= max(case '+@s2+' end)', @s3=',[AGE]= max(case '+@s3+' end)'--插入记录:
insert #2 exec('select row'+@s+@s2+@s3+'
from
(select * ,row=(select count(1) from # where CITY=t.CITY and ID!>t.ID )
from # t)Tmp
group by row')go
select * from #2ID 上海 北京 深圳 NAME AGE
----------- ----- ----- ----- ---- -----------
1 上海 北京 深圳 Y 24
2 上海 NULL NULL Y 24
3 上海 NULL NULL Z 24(所影响的行数为 3 行)
insert # select 1, '上海', 'X', 23
insert # select 2, '北京', 'X', 23
insert # select 3, '上海', 'Y', 24
insert # select 4, '深圳', 'Y', 24
insert # select 5, '上海', 'Z', 24 go
这个,我已经有表了就不用再建了吧?
普通行列转换(2007-11-18于海南三亚)假设有张学生成绩表(tb)如下:
Name Subject Result
张三 语文 74
张三 数学 83
张三 物理 93
李四 语文 74
李四 数学 84
李四 物理 94
*/-------------------------------------------------------------------------
/*
想变成
姓名 语文 数学 物理
---------- ----------- ----------- -----------
李四 74 84 94
张三 74 83 93
*/create table tb
(
Name varchar(10) ,
Subject varchar(10) ,
Result int
)insert into tb(Name , Subject , Result) values('张三' , '语文' , 74)
insert into tb(Name , Subject , Result) values('张三' , '数学' , 83)
insert into tb(Name , Subject , Result) values('张三' , '物理' , 93)
insert into tb(Name , Subject , Result) values('李四' , '语文' , 74)
insert into tb(Name , Subject , Result) values('李四' , '数学' , 84)
insert into tb(Name , Subject , Result) values('李四' , '物理' , 94)
go--静态SQL,指subject只有语文、数学、物理这三门课程。
select name 姓名,
max(case subject when '语文' then result else 0 end) 语文,
max(case subject when '数学' then result else 0 end) 数学,
max(case subject when '物理' then result else 0 end) 物理
from tb
group by name
/*
姓名 语文 数学 物理
---------- ----------- ----------- -----------
李四 74 84 94
张三 74 83 93
*/--动态SQL,指subject不止语文、数学、物理这三门课程。
declare @sql varchar(8000)
set @sql = 'select Name as ' + '姓名'
select @sql = @sql + ' , max(case Subject when ''' + Subject + ''' then Result else 0 end) [' + Subject + ']'
from (select distinct Subject from tb) as a
set @sql = @sql + ' from tb group by name'
exec(@sql)
/*
姓名 数学 物理 语文
---------- ----------- ----------- -----------
李四 84 94 74
张三 83 93 74
*/-------------------------------------------------------------------
/*加个平均分,总分
姓名 语文 数学 物理 平均分 总分
---------- ----------- ----------- ----------- -------------------- -----------
李四 74 84 94 84.00 252
张三 74 83 93 83.33 250
*/--静态SQL,指subject只有语文、数学、物理这三门课程。
select name 姓名,
max(case subject when '语文' then result else 0 end) 语文,
max(case subject when '数学' then result else 0 end) 数学,
max(case subject when '物理' then result else 0 end) 物理,
cast(avg(result*1.0) as decimal(18,2)) 平均分,
sum(result) 总分
from tb
group by name
/*
姓名 语文 数学 物理 平均分 总分
---------- ----------- ----------- ----------- -------------------- -----------
李四 74 84 94 84.00 252
张三 74 83 93 83.33 250
*/--动态SQL,指subject不止语文、数学、物理这三门课程。
declare @sql1 varchar(8000)
set @sql1 = 'select Name as ' + '姓名'
select @sql1 = @sql1 + ' , max(case Subject when ''' + Subject + ''' then Result else 0 end) [' + Subject + ']'
from (select distinct Subject from tb) as a
set @sql1 = @sql1 + ' , cast(avg(result*1.0) as decimal(18,2)) 平均分,sum(result) 总分 from tb group by name'
exec(@sql1)
/*
姓名 数学 物理 语文 平均分 总分
---------- ----------- ----------- ----------- -------------------- -----------
李四 84 94 74 84.00 252
张三 83 93 74 83.33 250
*/drop table tb ---------------------------------------------------------
---------------------------------------------------------
/*
如果上述两表互相换一下:即姓名 语文 数学 物理
张三 74 83 93
李四 74 84 94想变成
Name Subject Result
---------- ------- -----------
李四 语文 74
李四 数学 84
李四 物理 94
张三 语文 74
张三 数学 83
张三 物理 93
*/create table tb1
(
姓名 varchar(10) ,
语文 int ,
数学 int ,
物理 int
)insert into tb1(姓名 , 语文 , 数学 , 物理) values('张三',74,83,93)
insert into tb1(姓名 , 语文 , 数学 , 物理) values('李四',74,84,94)select * from
(
select 姓名 as Name , Subject = '语文' , Result = 语文 from tb1
union all
select 姓名 as Name , Subject = '数学' , Result = 数学 from tb1
union all
select 姓名 as Name , Subject = '物理' , Result = 物理 from tb1
) t
order by name , case Subject when '语文' then 1 when '数学' then 2 when '物理' then 3 when '总分' then 4 end--------------------------------------------------------------------
/*加个平均分,总分
Name Subject Result
---------- ------- --------------------
李四 语文 74.00
李四 数学 84.00
李四 物理 94.00
李四 平均分 84.00
李四 总分 252.00
张三 语文 74.00
张三 数学 83.00
张三 物理 93.00
张三 平均分 83.33
张三 总分 250.00
*/select * from
(
select 姓名 as Name , Subject = '语文' , Result = 语文 from tb1
union all
select 姓名 as Name , Subject = '数学' , Result = 数学 from tb1
union all
select 姓名 as Name , Subject = '物理' , Result = 物理 from tb1
union all
select 姓名 as Name , Subject = '平均分' , Result = cast((语文 + 数学 + 物理)*1.0/3 as decimal(18,2)) from tb1
union all
select 姓名 as Name , Subject = '总分' , Result = 语文 + 数学 + 物理 from tb1
) t
order by name , case Subject when '语文' then 1 when '数学' then 2 when '物理' then 3 when '平均分' then 4 when '总分' then 5 enddrop table tb1
insert # select 1, '上海', 'X', 23
insert # select 2, '北京', 'X', 23
insert # select 3, '上海', 'Y', 24
insert # select 4, '深圳', 'Y', 24
insert # select 5, '上海', 'Z', 24 go
declare @s nvarchar(4000),@s2 nvarchar(4000),@s3 nvarchar(4000)
select @s='',@s2='',@s3=''
select @s=@s+','+quotename(CITY)+'=max(case when CITY='+quotename(CITY,'''')
+' then '+quotename(CITY,'''')+' end)',
@s2=@s2+' when CITY='+quotename(CITY,'''')+' then NAME ',
@s3=@s3+' when CITY='+quotename(CITY,'''')+' then AGE 'from
# t
group by CITY
order by (select min(ID) from # where CITY=t.CITY) asc--排序select @s2=',[Name]= max(case '+@s2+' end)', @s3=',[AGE]= max(case '+@s3+' end)'exec('select identity(int,1,1) as id '+@s+@s2+@s3+' into ##2 from # group by name,age')
go
select * from ##2
drop table #,##2
/*id 上海 北京 深圳 Name AGE
----------- ---- ---- ---- ---- -----------
1 上海 北京 NULL X 23
2 上海 NULL 深圳 Y 24
3 上海 NULL NULL Z 24
*/