--查询
select ID
,Col1=max(case Item when 'Col1' then Value end)
,Col2=max(case Item when 'Col2' then Value end)
from 表
group by ID

解决方案 »

  1.   

    --测试--测试数据
    create table 表(ID int,Item varchar(10),Value varchar(10))
    insert 表 select 1,'Col1','A'
    union all select 1,'Col2','12'
    union all select 2,'Col1','A'
    union all select 2,'Col2','15'
    union all select 3,'Col1','B'
    union all select 3,'Col2','12'
    union all select 4,'Col1','B'
    union all select 4,'Col2','13'
    go--查询
    select ID
    ,Col1=max(case Item when 'Col1' then Value end)
    ,Col2=max(case Item when 'Col2' then Value end)
    from 表
    group by ID
    go--删除测试
    drop table 表/*--测试结果
    ID          Col1       Col2       
    ----------- ---------- ---------- 
    1           A          12
    2           A          15
    3           B          12
    4           B          13(所影响的行数为 4 行)--*/
      

  2.   

    select i_id ,max((case v_itm when 'col1' then v_value end)) col1,max((case v_itm when 'col2' then v_value end)) col2 from @tbl
    group by i_id
      

  3.   

    Col1=max(case Item when 'Col1' then Value end)
    Col2=max(case Item when 'Col2' then Value end)可否解释这两句是什么意思呢?小弟看不明白。