日期:2014-05-18  浏览次数:20455 次

求个简单的SQL语句
select a,b from tab1 inner join tab2 
on tab1.c=tab2.c 
where b=1or b=2or b=3
结果如下:
  a b
--------------------------
  zs 1
  zs 2
  zs 3
  zs 4
  ww 2
  ww 3
  de 1
  de 3
  de 4

我想要这样的结果:
  a 1 2 3 4
------------------------------------------------------
  zs 1 2 3 4
  ww null 2 3 null
  de 1 null 3 4

怎么写语句?

------解决方案--------------------
SQL code
select
    a,
    [1]=max(case b when 1 then 1 end),
    [2]=max(case b when 2 then 2 end),
    [3]=max(case b when 3 then 3 end),
    [4]=max(case b when 4 then 4 end)
from 
    tab1 inner join tab2  
on 
    tab1.c=tab2.c  
where 
    b=1 or b=2 or b=3
group by
    a