--建立测试环境
Create Table tb1(mcht_id varchar(10),amount integer)
--插入数据
insert into tb1
select 'a','1000' union
select 'c','130' union
select 'd','200' union
select 'b','120' union
select 'b','80'
Create Table tb2(mcht_id varchar(10),mcht_type varchar(10))
--插入数据
insert into tb2
select 'a','01' union
select 'd','01' union
select 'b','02'
--测试语句
  select isnull(mcht_type,'01')mcht_type,sum(amount) from
tb1 left join tb2 on tb1.mcht_id=tb2.mcht_id
group by isnull(mcht_type,'01')
 
 
--删除测试环境
Drop Table tb2
Drop Table tb1

解决方案 »

  1.   

    jinjazz(近身剪*10年磨一贴)厉害,改一点。Select IsNull(mcht_type,'01') As mcht_type,SUM(amount) As amount from tb1 
    Left Join tb2 On tb1.mcht_id=tb2.mcht_id
    Group By IsNull(mcht_type,'01')
      

  2.   


    tb3存在的话
    Insert tb3
    Select IsNull(mcht_type,'01') As mcht_type,SUM(amount) As amount from tb1 
    Left Join tb2 On tb1.mcht_id=tb2.mcht_id
    Group By IsNull(mcht_type,'01')tb3不存在的话
    Select IsNull(mcht_type,'01') As mcht_type,SUM(amount) As amount  Into tb3 from tb1 
    Left Join tb2 On tb1.mcht_id=tb2.mcht_id
    Group By IsNull(mcht_type,'01')