select 凭证号,count(记录号) sum(金额)  类型 from table where 类型='a' and 凭证号='001'
select 凭证号,count(记录号) sum(金额)  类型 from table where 类型='b' and 凭证号='001'
问题是如何将这两条记录合并呢

解决方案 »

  1.   

    select a.凭证号,count(distinct a.记录号), sum(distinct a.金额), a.类型,count( distinct b.记录号), sum( distinct b.金额), b.类型
    from 表 a join 表 b on a.凭证号=b.凭证号 
    where a.类型='a' and b.类型='b'
    group by a.凭证号,a.类型,b.类型
      

  2.   

    select 凭证号,count(记录号) sum(金额)  类型 from table where 类型='a' and 凭证号='001'
    select 凭证号,count(记录号) sum(金额)  类型 from table where 类型='b' and 凭证号='001' group by 类型
    表a和b自然连接