a结构a1,a2,a3
b结构b1,b2,b3,b4,b5
我的sql语句(a记录为5条,b记录为2条)
select 
    a.a1, 
    sum(case  when  b.b2=1  then b.b3 else 0 end)  
    sum(case  when  b.b2=1  then b.b4 else 0 end )  
from 
    a left join b on (a.a1=b.b1)     
where 
    b.b5=100
group by 
    a.a1
问:
    因为有条件所以得不出5条记录,但是我必须要得出5条记录,请问这样得sql怎么写

解决方案 »

  1.   

    select 
        a.a1, 
        sum(case  when  b.b2=1 and b.b5=100 then b.b3 else 0 end)  ,
        sum(case  when  b.b2=1 and b.b5=100 then b.b4 else 0 end )  
    from 
        a left join b on (a.a1=b.b1)     
    group by 
        a.a1
      

  2.   

    select 
        a.a1, 
        sum(case  when  b.b2=1  then b.b3 else 0 end),  
        sum(case  when  b.b2=1  then b.b4 else 0 end )  
    from  a,b 
    wherea.a1*=b.b1 and b.b5=100
    group by a.a1
      

  3.   

    '对不起,刚才一楼忘了一打空格
    select 
        a.a1, 
        sum(case  when  b.b2=1  then b.b3 else 0 end),  
        sum(case  when  b.b2=1  then b.b4 else 0 end )  
    from  a,b 
    where a.a1*=b.b1 and b.b5=100
    group by a.a1
      

  4.   

    感觉是因为A表5条记录,B表2条记录,而你的Case When中没有加上当B表的值为Null的时候的情况,问题可能在这.