各位,
小弟现在有张表如下。考试日期   考生地区     考试科目
2013-09-30   北京         语文  
2013-09-30   北京         数学  
2013-09-30   上海         语文  
2013-09-30   山东         语文  
2013-09-30   河北         语文  
2013-09-29   河北         语文  2013-10-29   北京         语文  
2013-10-29   上海         数学  
2013-10-27   河北         数学  
2013-10-26   河北         数学   
最后我想出的报表 应该如下
年    月   地区   语文考试次数  数学考试次数
2013  9    北京        1             1
2013  10   北京        1             0
2013  9    上海        1             0
2013  10   上海        0             1
2013  9    河北        2             0
2013  10   河北        0             2
2013  9    山东        1             0应该就是 按照年、月、地区 分组,计算出 语文考试的次数,
但是麻烦在,还得将按照年、月、地区 分组,计算出 数学考试的次数 放在一起这我就晕了。
对了 最好别用 主子查询。
这样太慢了。求助报表count

解决方案 »

  1.   

    直接sum(decode()) group by不就完事了么
      

  2.   


    with t1 as
    (
         select date'2013-09-30' rq,'北京' dq,'语文' km from dual union all
         select date'2013-09-30' rq,'北京' dq,'数学' km from dual union all
         select date'2013-09-30' rq,'上海' dq,'语文' km from dual union all
         select date'2013-09-30' rq,'山东' dq,'语文' km from dual union all
         select date'2013-09-30' rq,'河北' dq,'语文' km from dual union all
         select date'2013-09-29' rq,'河北' dq,'语文' km from dual union all
         select date'2013-10-30' rq,'北京' dq,'语文' km from dual union all
         select date'2013-10-29' rq,'上海' dq,'数学' km from dual union all
         select date'2013-10-27' rq,'河北' dq,'数学' km from dual union all
         select date'2013-10-26' rq,'河北' dq,'数学' km from dual
    )select to_char(rq,'yyyy') "年",to_char(rq,'mm') "月",dq "地区",
           sum(decode(km,'语文',1,0)) "语文次数",
           sum(decode(km,'数学',1,0)) "数学次数"
    from t1
    group by to_char(rq,'yyyy'),dq,to_char(rq,'mm')
    order by to_char(rq,'yyyy'),dq,to_char(rq,'mm')
         年    月    地区  语文次数   数学次数
    --------------------------------------------------
    1 2013 09 北京 1 1
    2 2013 10 北京 1 0
    3 2013 09 河北 2 0
    4 2013 10 河北 0 2
    5 2013 09 山东 1 0
    6 2013 09 上海 1 0
    7 2013 10 上海 0 1
      

  3.   

    哎呀. 高人啊.
    但是如果我在提高一点难度最后我想出的报表 应该如下
    年    月   地区   语文考试次数  除语文其他考试次数
    2013  9    北京        1             1
    2013  10   北京        1             0
    2013  9    上海        1             0
    2013  10   上海        0             1
    2013  9    河北        2             0
    2013  10   河北        0             2
    2013  9    山东        1             0主要是decode 我不知道 怎么写 不等于....
      

  4.   


    select to_char(rq,'yyyy') "年",to_char(rq,'mm') "月",dq "地区",
           sum(decode(km,'语文',1,0)) "语文次数",
           sum(decode(km,'语文',0,1)) "除语文其他考试次数"
    from t1
    group by to_char(rq,'yyyy'),dq,to_char(rq,'mm')
    order by to_char(rq,'yyyy'),dq,to_char(rq,'mm')