select id,
name=(case when ColumnName='Name' then max(ColumnValue) else null end),
Sex =(case when ColumnName='Sex'  then max(ColumnValue) else null end),
age =(case when ColumnName='age'  then max(ColumnValue) else null end)
from 表A group by id

解决方案 »

  1.   

    select id
    ,name=max(case when columnname='name' then columnvalue else '' end)
    ,sex=max(case when columnname='sex' then columnvalue else '' end)
    ,age=max(case when columnname='age' then columnvalue else '' end)
    from A
    group by id
      

  2.   

    select aa.id,name=(select colvalue from aa where id=aa.id and columnname='name'),
    sex=(select colvalue from aa where id=aa.id and columnname='sex'),
    age=(select colvalue from aa where id=aa.id and columnname='age') 
    from  (select distinct id from a) aa
      

  3.   

    select aa.id,name=(select columnvalue from aa where id=aa.id and columnname='name'),
    sex=(select columnvalue from aa where id=aa.id and columnname='sex'),
    age=(select columnvalue from aa where id=aa.id and columnname='age') 
    from  (select distinct id from a) aa   --列名少写了,加上去
      

  4.   

    你试试这样可以吗?????
    SELECT id,
              (SELECT ColumnValue
             FROM table
             where ColumnName='Name' ) AS Name,
              (SELECT ColumnValue
             FROM table
             where ColumnName='Sex' ) AS Sex,
              (SELECT ColumnValue
             FROM table
             where ColumnName='age' ) AS age
    FROM table
    GROUP BY id
      

  5.   

    create table A (Id int ,columnname varchar(10),columnvalue varchar(50))
    insert into A 
    select '1','Name', 'Peter'
    union select '1','Sex',  'male'
    union select '1','age' ,'17'union select '2','Name' ,'Tom'
    union select '2','Sex','male'
    union select '2','age','18'union select '3','Name','David'
    union select '3' ,'sex','male'
    union select '3','age','9'
    select * from A order by id--测试
    declare @sql varchar(8000);
    select @sql = 'select id '
    select @sql = @sql+',isnull(max(case when columnname = '''+columnname +'''then columnvalue end),'''') ['+columnname+']'
    from (select distinct columnname  from A ) B
    select @sql = @sql +' from A group by id'
    print @sql
    exec( @sql)/*测试结果*/
    id  age  name     Sex                                                
    1   17 Peter    male
    2   18 Tom male
    3   9 David male