ITPub博客

首页 > 数据库 > Oracle > 一道关于数据按照一定规律分列的SQL解法

一道关于数据按照一定规律分列的SQL解法

原创 Oracle 作者:hu825065235 时间:2014-07-23 09:23:06 0 删除 编辑
原题如下:


解答如下:

----第一题
select max(case
         when le = 1 then
          lv
       end) as c1,
       max(case
         when le = 2 then
          lv
       end) as c2,
       max(case
         when le = 0 then
          lv
       end) as c3
  from (select lv, mod(lv, 3) as le ,row_number()over(partition by mod(lv, 3)order by lv ) as rn
          from (select level as lv from dual connect by level <= 9))
          group by rn
 order by c1, c2, c3

---第二题
select max(case
         when le = 1 then
          lv
       end) as c1,
       max(case
         when le = 2 then
          lv
       end) as c2,
       max(case
         when le = 3 then
          lv
       end) as c3
  from (select lv, ceil(lv/ 4) as le ,row_number()over(partition by ceil(lv/4)order by lv ) as rn
          from (select level as lv from dual connect by level <= 9))
          group by rn
 order by c1, c2, c3

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29507357/viewspace-1225030/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2014-02-26

  • 博文量
    34
  • 访问量
    83413