表结构:created   table_a (
        p_id  number,
        m_code varchar2(100),
        q_receive  number,
);
表中数据如下:
 201201    S001   10
 201205    S001   5
 2012011   S001   20要求得到结果:
S001  1季度    10
S001  2季度    15
S001  3季度    15
S001  4季度    35求各位大神指点,在下感谢了先。

解决方案 »

  1.   


    with s as (
      select "1季度" as name, 201201 as begin_month, 201204 as end_month from dual
      union all
      select "2季度" as name, 201201 as begin_month, 201207 as end_month from dual
      union all
      select "3季度" as name, 201201 as begin_month, 201210 as end_month from dual
      union all
      select "4季度" as name, 201201 as begin_month, 201301 as end_month from dual
    )
    select t.m_code, s.name, sum(t.q_receive) from table_a t, s
    where t.p_id>=s.begin_month and t.p_id<s.end_month
    group by t.m_code, s.name order by t.m_code, s.name;