凡是这种问题不可用group by !

解决方案 »

  1.   

    select (select sum(借方) from 资金明细表 table1 
             where table1.科目='办公用品') as 办公费
          ,(select sum(借方) from 资金明细表 table2 
             where table2.科目='车费') as 车费
      .......
        ,(select sum(借方) from 资金明细表 tableN) as 总费用
        ,(select sum(借方) from 资金明细表 tableN1 
            where  tableN1.科目<>'车费' and  ...<>'办公用品’ ....)as 其他费用
      

  2.   

    select case 
        when  科目 in ('办公用品','车费','饭补','评审费','会议费') 
         then 科目 
        else '其他费用' 
        end,
        sum(借方) as 科目统计 
        from 资金明细表 
        group by case 
        when  科目 in ('办公用品','车费','饭补','评审费','会议费') 
         then 科目 
        else '其他费用' 
        end
      

  3.   

    试试看
    select 科目,sum(借方) as 科目统计 from 资金明细表
    where 科目='办公用品' or 科目='车费' or 科目='饭补' or 科目='评审费' or 科目='会议费' group by 科目
    union
    select '其它费用' as 科目,sum(借方) as 科目统计 from 资金明细表 where 
    (科目<>'办公用品' and 科目<>'车费' and 科目='饭补' and 科目='评审费' and 科目='会议费')
    union
    select '总费用' as 科目,sum(借方) as 科目统计 from 资金明细表
      

  4.   

    呵呵,原来还要总费用,yzx99(yzx99) 的答案应该是正确的。
    也可以这样:select case 
        when  科目 in ('办公用品','车费','饭补','评审费','会议费') 
         then 科目 
        else '其他费用' 
        end,
        sum(借方) as 科目统计 
        from 资金明细表 
        group by case 
        when  科目 in ('办公用品','车费','饭补','评审费','会议费') 
         then 科目 
        else '其他费用' 
        end
    union
    select '总费用' as 科目,sum(借方) as 科目统计 from 资金明细表
      

  5.   

    select 科目,sum(借方) from your_tab group by cube(科目);
    自己试吧,实在没时间了