2道经典的oracle查询案例

第一题:

2道经典的oracle查询案例_第1张图片

 

第一题:

直接贴代码:
select Id,Name,Money,(select Money from test1 a where a.Id = decode(b.Id - 1,0,null,b.Id-1)) Money1 from test1 b;  
经典的子查询,注意的就是null值的处理问题,decode或者case是oracle很重要的单行函数。

例如:涨工资的例子: select ename,job,sal 涨前, decode(job,'PRESIDENT',sal+1000, 'MANAGER',sal+800, sal+400) 涨后 from emp;
等同于:select ename,job,sal 涨前,case job when 'PRESIDENT' then sal+1000
                    when 'MANAGER' then sal+800
                    else sal+400
            end 涨后
            from emp;

 第二题: 2道经典的oracle查询案例_第2张图片

代码: select CI_ID,wm_concat(STU_NAME) from ( select CI_ID, STU_NAME
      from  pm_ci p1, pm_stu p2 
     where
                 STU_ID = to_number(substr(p1.STU_IDS,1,1)) or
                 STU_ID = to_number(substr(p1.STU_IDS,3,1)) or
                 STU_ID = to_number(substr(p1.STU_IDS,5,1)) or
                 STU_ID = to_number(substr(p1.STU_IDS,7,1)) 
    ) group by CI_ID;          //行转列函数 wm_concat ,还有一个思路就是 in方法,把1,2,3,4放在in()里面,这里就不演示了。

你可能感兴趣的:(2道经典的oracle查询案例)