有如下一张表:
  user  action
  张三     A
  张三     A
  张三     B
  李四     A
  王五     A
  王五     B
  赵六     B
  赵六     B
  赵六     B解释一下:操作总共有A,B两种,一个用户可能多次操作A或B,也可能只操作了其中一种,另一种完全没操作过,现在要统计的是用户对两种操作的操作次数,即要统计到的结果表如下:
  user  actionA actionB
  张三     2        1
  李四     1        0
  王五     1        1
  赵六     0        3请问这个SQL语句要怎么写呢?
谢谢您的解答!

解决方案 »

  1.   

    select user,sum(decode(action,'A',1,0)) actionA,sum(decode(action,'B',1,0)) actionB
    from table
    group by user
      

  2.   

    select nvl(t2.usr, t3.usr) usr, nvl(t2.actionA, 0) actionA, nvl(t3.actionB, 0) actionB
      from (select usr, count(*) actionA
              from test01
             where action = 'A'
             group by usr) t2
      full outer join (select usr, count(*) actionB
                         from test01
                        where action = 'B'
                        group by usr) t3 on t2.usr = t3.usr;
      

  3.   

    select sum(decode(action,'A',1,0)) actionA, sum(decode(action,'B',1,0)) actionB from test group by user