现在表结构是这样的   字段1        字段2  字段3  字段4  字段5
   test       aa     bb    1     XXX
   test       aa     bb    1     XXXX
   test       aa     bb    1     XXXXXX
   test       cc     dd    2     XX
   test       cc     dd    2     XXX
   test       cc     dd    2     XXXX查询想要的效果是   test       aa     bb    cc    dd  
SQL要怎么写

解决方案 »

  1.   


    with tb(a,b,c,d,e)as(
    select 'test','aa','bb',1,'xxx' union all
    select 'test','aa','bb',1,'xxxx' union all
    select 'test','aa','bb',1,'xxxxxx' union all
    select 'test','cc','dd',2,'xx' union all
    select 'test','cc','dd',2,'xxx' union all
    select 'test','cc','dd',2,'xxxx' 
    )
    select a,max(case when d=1 then b else null end)
            ,max(case when d=1 then c else null end)
            ,max(case when d=2 then b else null end)
            ,max(case when d=2 then c else null end) from tb a
            group by a
    这样?