表t1中有a,b两字段
表中数据如下
  a            b
  1           ssas
  2           jjbj
  1           ddas
  如果查询按字段a分组,按a=1时字段b中包含a的总数,a=2时字段b中包含b的总数,可不可以得出下面的数据。谢谢!
 aa    count
 1     2
 2     1

解决方案 »

  1.   

    试试下面可以达到你要的结果
    select a,count(a) count from t1 group by a
      

  2.   

    有点乱,不知道理解的对不对select a,sum(case a when 1 then (case instr(b,'a') when 0 then 0 else 1 end) when 2 then (case instr(b,'b') when 0 then 0 else 1 end) end)
    from t1
    group by a
      

  3.   

    shark2004(伤心小丑) 是对的
      

  4.   

    我好意思理解错了,我问一下你的记录只有这两种对应关系吗?a=1 对应b中包含a,a=2
    b中包含b
      

  5.   

    select a,sum(case a when 1 then (case instr(b,'a') when 0 then 0 else 1 end) when 2 then (case instr(b,'b') when 0 then 0 else 1 end) end)
    from t1
    group by a对了