62,050
社区成员
发帖
与我相关
我的任务
分享
动态的
create table #A1([ID] int,[Code] varchar(3),[Name] varchar(4))
insert #A1
select 1,'001','张三' union all
select 2,'002','李四'
create table #B([Code] varchar(3),[Type] varchar(4),[Result] varchar(7))
insert #B
select '001','重量','100公斤' union all
select '001','身高','170CM' union all
select '002','视力','0.5'
select A.ID,A.[Code],A.[Name],
max(case when [Type]='重量' then [Result] else null end) '重量',
max(case when [Type]='身高' then [Result] else null end) '身高',
max(case when [Type]='视力' then [Result] else null end) '视力'
from #A1 A join #B B on A.[Code]=B.[Code] group by A.ID,A.[Code],A.[Name]
declare @sql nvarchar(2000)
set @sql='select A.ID,A.[Code],A.[Name],'
select @sql=@sql+'max(case when [Type]='''+[Type]+'''then [Result] else null end) ['+[Type]+'],' from #A1 A join #B B on A.[Code]=B.[Code]
select @sql=left(@sql,len(@sql)-1)+'from #A1 A join #B B on A.[Code]=B.[Code] group by A.ID,A.[Code],A.[Name]'
exec(@sql)
ID Code Name 重量 身高 视力
----------- ---- ---- ------- ------- -------
1 001 张三 100公斤 170CM NULL
2 002 李四 NULL NULL 0.5
静态的
create table #A1([ID] int,[Code] varchar(3),[Name] varchar(4))
insert #A1
select 1,'001','张三' union all
select 2,'002','李四'
create table #B([Code] varchar(3),[Type] varchar(4),[Result] varchar(7))
insert #B
select '001','重量','100公斤' union all
select '001','身高','170CM' union all
select '002','视力','0.5'
select A.ID,A.[Code],A.[Name],
max(case when [Type]='重量' then [Result] else null end) '重量',
max(case when [Type]='身高' then [Result] else null end) '身高',
max(case when [Type]='视力' then [Result] else null end) '视力'
from #A1 A join #B B on A.[Code]=B.[Code] group by A.ID,A.[Code],A.[Name]
ID Code Name 重量 身高 视力
----------- ---- ---- ------- ------- -------
1 001 张三 100公斤 170CM NULL
2 002 李四 NULL NULL 0.5
create table A(ID int null,Code nvarchar(3) null,[Name] nvarchar(4) null)
insert into A(ID,Code,[Name])
select 1,'001','张三'
union all
select 2,'002','李四'
create table B(Code nvarchar(3) null,[Type] nvarchar(4) null,Result nvarchar(7) null)
insert into B(Code,[Type],Result)
select '001','重量','100公斤'
union all
select '001','身高','170CM'
union all
select '002','视力','0.5'
select A.ID,A.Code,A.[Name],max(case B.[Type] when '重量' then Result else null end)as 重量,
max(case B.[Type] when '身高' then Result else null end)as 身高,
max(case B.[Type] when '视力' then Result else null end)as 视力
from A inner join B on A.code=B.code group by A.ID,A.Code,A.[Name]
ID Code Name 重量 身高 视力
----------- ---- ---- ------- ------- -------
1 001 张三 100公斤 170CM NULL
2 002 李四 NULL NULL 0.5
(2 個資料列受到影響)
-----------------------------------------
--> 测试时间:2009-07-13
--> 我的淘宝:http://shop36766744.taobao.com/
--------------------------------------------------
if object_id('[A1]') is not null drop table [A1]
create table [A1]([ID] int,[Code] varchar(3),[Name] varchar(4))
insert [A1]
select 1,'001','张三' union all
select 2,'002','李四'
if object_id('[B2]') is not null drop table [B2]
create table [B2]([Code] varchar(3),[Type] varchar(4),[Result] varchar(7))
insert [B2]
select '001','重量','100公斤' union all
select '001','身高','170CM' union all
select '002','视力','0.5'
select * from [A1]
select * from [B2]
declare @s nvarchar(4000)
Select @s=isnull(@s+',','')+quotename([Type])+'=max(case when [Type]='+quotename([Type],'''')+' then [Result] else '''' end)'
from B2 group by[Type]
exec('select ID,A1.[Code],Name,'+@s+' from A1 join B2 on A1.Code=B2.Code group by A1.[Code],ID,Name')
/*
ID Code Name 身高 视力 重量
----------- ---- ---- ------- ------- -------
1 001 张三 170CM 100公斤
2 002 李四 0.5
*/
drop table A1,B1