select c.typename,isnull(sum(b.num*a.price),0) as amount
from tb1 a join tb2 b on a.id=b.id
           right join tb3 c on a.type=c.type  
group by c.typename

解决方案 »

  1.   

    select c.typename,sum(isnull(b.num,0)*isnull(a.price,0))
    from tb3 c
    left join tb1 a on a.type=c.type
    join tb2 b on a.id=b.id
    group by c.name
      

  2.   

    现在可以了,但我要加一个where tb1.abc='c'这个条件时,就只显示tb1.type中有的记录了。这要怎么改啊?谢谢。
      

  3.   

    无论是否符合where tb1.abc='c'的条件,tb3.type的数据也要显示出来,sum(tb2.num * tb1.price)的结果没有记录时显示为0
      

  4.   

    select c.typename,sum(isnull(b.num,0)*isnull(a.price,0))
    from tb3 c
    left join tb1 a on a.type=c.type
    join tb2 b on a.id=b.id
    where isnull(a.abc,'c')='c'  --这样加
    group by c.name
      

  5.   

    请问isnull(a.abc,'c')='c' 中的这两个c分别是什么意思?我把这两个c都改为a了后,tb3.type的数据就没有全部显示出来了,只显示了一条,希望改为a也全部显示。
      

  6.   

    意思是,如果a.abc的值为NULL,则返回c,否则返回a.abc的值,然后与c做比较
      

  7.   

    因为a.abc的值是动态产生的,如果我把a.abc改为a或其他了后,就只显示一条tb3.type的数据了。但是我想tb3.type的数据都要显示出来,sum(tb2.num * tb1.price)的结果没有记录时显示为0