| 发表于:2007-06-25 10:49:1618楼 得分:5 |
--准备数据 declare @tab table( [id] int ,code int ,classname nvarchar(10) ,classvalue nvarchar(10) ) insert into @tab select 1, 100, '姓名 ', '马志远 ' union all select 2, 100, '性别 ', '男 ' union all select 3, 100, '年龄 ', '19 ' union all select 4, 101, '姓名 ', '李志 ' union all select 5, 101, '性别 ', '男 ' union all select 6, 101, '年龄 ', '21 ' union all select 7, 102, '姓名 ', '王强 ' union all select 8, 102, '性别 ', '男 ' union all select 9, 102, '年龄 ', '20 ' --原始表 select * from @tab --查询 select distinct a.code ,b.classvalue as '姓名 ' ,c.classvalue as '性别 ' ,d.classvalue as '年龄 ' from @tab a left outer join @tab b on b.classname= '姓名 ' and a.code=b.code left outer join @tab c on c.classname= '性别 ' and a.code=c.code left outer join @tab d on d.classname= '年龄 ' and a.code=d.code ===结果 100 马志远 男 19 101 李志 男 21 102 王强 男 20 | | |
|