用户名称 点击标题种类A的数目 点击标题种类B的数目 点击标题种类c的数目 点击标题总数(标题1和标题2的总数)
  1      2                    1                   0                   3
  2      1                    1                   0                   2
  3      0                    0                   1                   1
从一张表tableA中统计出来
tableA中字段:
 用户名称  标题名称  标题种类 
   1        tt        A
   1        ss        A
   1        kk        B
   2        tt1       A
   2        kk        B
   3        rr        c
不知道这个sql语句怎么写???请问一个sql语句还能搞定啊??前台是jsp的

解决方案 »

  1.   

    select t1.u,sum(decode(t2.c,'A',t2.s,0)),sum(decode(t2.c,'B',t2.s,0)),
    sum(decode(t2.c,'C',t2.s,0)),sum(t2.s) from 
    (select distinct u from a) t1,
    (select distinct u,c,count(*)over (partition by u,c) as s from a) t2
    where t1.u=t2.u group by t1.u
      

  2.   

    假设你的tableA列名如下
    用户名称:user_name
    标题名称:title_name
    标题种类:title_type这样写该就没问题了。
    SELECT user_name,
           SUM(DECODE(title_type,A,1,0)),
           SUM(DECODE(title_type,B,1,0)),
           SUM(DECODE(title_type,C,1,0)),
           COUNT(*)
      FROM tableA
     GROUP BY user_name;
      

  3.   

    如果你的表里只有A、B、C三类,采用decode可以实现,如果分类数目不定,则需要写存储过程来实现。