日期:2014-05-17  浏览次数:20649 次

oracle 查询求助!!
表结构: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

求各位大神指点,在下感谢了先。

------解决方案--------------------
基本查询语句,变换一下查询顺序 及 输出格式 即可
------解决方案--------------------

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;