Welcome 微信登录

首页 / 数据库 / MySQL / Oracle 根据行列号取值

//数据  
 A   B  C  
--  -- --  
a1  b1 c1  
a2  b2 c2  
a3  b3 c3  
//结果:  
row  col  value  
--   ---  -----  
 1     1  a1  
 1     2  b1  
 1     3  c1  
 2     1  a2  
 2     2  b2  
 2     3  c2  
 3     1  a3  
 3     2  b3  
 3     3  c3  
--  
with tb as(  
     select "a1" a,"b1" b,"c1" c from dual union all  
     select "a2","b2","c2" from dual union all  
     select "a3","b3","c3" from dual)  
select rn,1,a from (select a,b,c,rownum as rn from tb )  
union   
select rn,2,b from (select a,b,c,rownum as rn from tb )  
union   
select rn,3,c from (select a,b,c,rownum as rn from tb )  
--  
//解析:  
//通过执行子查询,从结果中我们看到了,对于每一行都返回了一个rn(每行行号);  
//对于每一个行号,执行上面的select union查询,每一次union,都返回一个结果集;  
//连接指定的1,2,3,和每一个rn对应的a,b,c值,就得到了我们想要的结果。  
//例如rn=1时:  
 1     1  a1  
 1     2  b1  
 1     3  c1  
--  
SQL> with tb as(  
  2       select "a1" a,"b1" b,"c1" c from dual union all  
  3       select "a2","b2","c2" from dual union all  
  4       select "a3","b3","c3" from dual)  
  5       select a,b,c,rownum as rn from tb  
  6  /  
A  B  C          RN  
-- -- -- ----------  
a1 b1 c1          1  
a2 b2 c2          2  
a3 b3 c3          3  Oracle 解决匹配的几种方法Oracle 构造与已知表结构相同的表问题相关资讯      Oracle教程 
  • Oracle中纯数字的varchar2类型和  (07/29/2015 07:20:43)
  • Oracle教程:Oracle中查看DBLink密  (07/29/2015 07:16:55)
  • [Oracle] SQL*Loader 详细使用教程  (08/11/2013 21:30:36)
  • Oracle教程:Oracle中kill死锁进程  (07/29/2015 07:18:28)
  • Oracle教程:ORA-25153 临时表空间  (07/29/2015 07:13:37)
  • Oracle教程之管理安全和资源  (04/08/2013 11:39:32)
本文评论 查看全部评论 (0)
表情: 姓名: 字数