当前位置:首页>开发>正文

oracle的行列转换,怎么实现 oracle行转列 列转行求助

2023-05-21 09:41:51 互联网 未知 开发

 oracle的行列转换,怎么实现 oracle行转列 列转行求助

oracle的行列转换,怎么实现?

--行转列的3种方法,参考
--http: / / blog.sina.com.cn/s/blog_475839a50100s2q3.html 
--1. UNION ALL
--适用范围:8i,9i,10g及以后版本
with tmp as(
select 123 as a, 456 as b, 789 as c from dual)
SELECT a 新列名 FROM tmp
UNION ALL
SELECT b FROM tmp
UNION ALL
SELECT c FROM tmp
--若空行不需要转换,只需加一个where条件,
--WHERE COLUMN IS NOT NULL 即可。

--2. MODEL
--适用范围:10g及以后
with tmp as(
select 123 as a, 456 as b, 789 as c from dual)
SELECT v 新列名 FROM tmp
MODEL
RETURN UPDATED ROWS
DIMENSION BY (0 AS n)
MEASURES (xx AS cn,yyy AS v,a,b,c)
RULES UPSERT ALL(
v[1] = a[0],
v[2] = b[0],
v[3] = c[0]
)

--3. COLLECTION
--适用范围:8i,9i,10g及以后版本
--要创建一个对象和一个集合:
CREATE OR REPLACE TYPE v_type AS OBJECT(v VARCHAR2(100))
CREATE OR REPLACE TYPE v_varr AS VARRAY(8) OF v_type
with tmp as(
select 123 as a, 456 as b, 789 as c from dual)
SELECT t.v AS 新列名
FROM tmp,
TABLE(v_varr(v_type(tmp.a),v_type(tmp.b),v_type(tmp.c))) t

oracle行转列 列转行求助

select id, substr( names, instr(|||| names, ||, 1, rn),
instr( names||||, ||, 1, rn) - instr(|||| names, ||, 1, rn) ) name
from A,(select rownum rn from dual connect by rownum < 10)
where instr(|||| names, ||, 1, rn) > 0

oracle sql 中 如何实现table的行列转换?

你所谓的行列转换应该是指纵表转横表,横表转纵表.
给你个例子
纵表转横表:
使用DECODE语句,可以很方便的将纵表转为横表,例子如下:
原表查询结果:
ID MAJOR CURRENT_CREDITS
------ ---------------- ---------------
10000 Computer Science 98
10000 History 88
10001 Computer Science 75
10000 Economics 66
我们要把各科成绩从同一列转到不同的列中去。
SQL>?select id,
sum(decode(major,’Computer Science’,current_credits,0)) cs,
sum(decode(major,’History’,current_credits,0)) his,
sum(decode(major,’Economics’,current_credits,0)) eco
from students
group by id
ID CS HIS ECO
------ ----------- ------------- -----
10000 98 88 66
10001 75
横表转纵表:
使用 UNION 即可实现将横表转为纵表,以上面的表为例:
转换前:
ID CS HIS ECO
------ ----------- ------------- -----
10000 98 88 66
SQL>?select id, ’Computer Science’ major,cs current_credits
from students
union
select id, ’History’ major,his current_credits
from students
union
select id, ’Economics’ major,eco current_credits
from students
ID MAJOR CURRENT_CREDITS
------ ---------------- ---------------
10000 Computer Science 98
10000 History 88
10000 Economics 66

oracle 数据库行列转换问题

select B 列x,
   (select b from tabname where a=2011) 列2011,
    (select b from tabname where a=2012) 列2012,
    ...
from dual
union 
select C 列x,
   (select c from tabname where a=2011) 列2011,
    (select c from tabname where a=2012) 列2012,
    ...
from dual

oracle列转行


INSERT INTO 新表 (SELECT ID, 呼吸系统疾病 FROM 原表 WHERE 呼吸系统疾病有无 = 1 UNION SELECT ID, 消化系统疾病 FROM 原表 WHERE 消化系统疾病有无 = UNION SELECT ID, 循环系统疾病 FROM 原表 WHERE 循环系统疾病有无 = 1)
如果使用行转列,还得使用merge into,还不如上面的语句清晰。

oracle 行和列转换

如果你要做转换查询,真心劝你不要这么干,我的写法很麻烦,一张表不停的查询,逻辑读肯定搞得要死。
如果是往下面这张表灌数,那么可以写循环,慢慢来一行对应一列,可以根据表的列和数据的对应关系往里面灌,这个相对简单些。
我的写法大概是子查询 union all 上面为a表
那么就写为select ‘第一季度 销售额,(select 第一季度销售额 from a where 产品名称=奶酪)奶酪,(select 第一季度销售额 from a where 产品名称=啤酒) from dual
union all
还像上面那么写,写第二季度
union all
第三季度
union all
第四季度
一张表重读查询8次,如果表很大,我估计机器会宕掉的。
因为单独从一列来看也可以理解为列转行,所以用case when写也可以,这么写似乎读取的次数会少些,不过要用到group by分组,天知道二者最后谁的消耗大。不过如果表很大的话,还是那句话,建议新建表然后灌数,这么直接查,真的会死掉的。
以上为个人建议,如果找到什么好写法,也可以研究下。

oracle查询结果怎么行列转换

看例子

SQL> select * from test

CTIME A B C
-------------- ---------- ---------- ----------
02-4月 -15 1
SQL> select * from test unpivot(value for profile in (A,B,C))

CTIME P VALUE
-------------- - ----------
02-4月 -15 A 02-4月 -15 B 2

最新文章