例如一个表中有多个种类的东西,每个种类又分1,2两种情况,统计每个种类这两种情况各是多少
用sql统计,有什么好的解决方案么

解决方案 »

  1.   


    select "种类", 
           sum(case when "子类" = '1' then 1 else 0 end) as "子类1次数",
           sum(case when "子类" = '2' then 1 else 0 end) as "子类1次数"
      from tbl 
     group by "种类";
      

  2.   


    with t as(
         select 'A' tp , 'a1' stp, 50 amount from dual
         union all
         select 'A' tp , 'a2' stp, 60 amount from dual
         union all
         select 'A' tp , 'a2' stp, 70 amount from dual
         union all
         select 'B' tp , 'b1' stp, 80 amount from dual
         union all
         select 'B' tp , 'b1' stp, 90 amount from dual 
    )select t.tp,t.stp,sum(t.amount),count(1)
    from t
    group by t.tp,t.stp
    order by t.tp,t.stp
      

  3.   


    select Category,SubCategory,sum(decode(SubCategory,'1', 1,'2',1,0))  SumSubCategory
        from tbl 
     group by Category,SubCategory;