RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
oracle行列怎么转换,Oracle行列转换

oracle 行列转换问题

方法一:最好理解

创新互联建站于2013年创立,是专业互联网技术服务公司,拥有项目成都网站设计、网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元合水做网站,已为上家服务,为合水各地企业和个人服务,联系电话:028-86922220

select t.指标名

,sum(decode(t.工厂,'一厂',t.指标值,0)) 一厂

,sum(decode(t.工厂,'二厂',t.指标值,0)) 二厂

from 数据表 t

group by t.指标名

方法二:用oracle11g新特性Pivot,也很好理解

select 指标名,"一厂","二厂"

from 数据表 t

pivot (sum(t.指标值) for "工厂" in ('一厂' as "一厂",'二厂' as "二厂"))

怎样将oracle中的列变成行

固定列数的行列转换如

student subject grade

---------------------------

student1 语文 80

student1 数学 70

student1 英语 60

student2 语文 90

student2 数学 80

student2 英语 100

转换为

语文 数学 英语

student1 80 70 60

student2 90 80 100

语句如下:

select student,sum(decode(subject,'语文', grade,null)) "语文",

sum(decode(subject,'数学', grade,null)) "数学",

sum(decode(subject,'英语', grade,null)) "英语"

from table

group by student

2、不定列行列转换如

c1 c2

--------------

1 我

1 是

1 谁

2 知

2 道

3 不

......

转换为

1 我是谁

2 知道

3 不

这一类型的转换必须借助于PL/SQL来完成,这里给一个例子

CREATE OR REPLACE FUNCTION get_c2(tmp_c1 NUMBER)

RETURN VARCHAR2

IS

--用于返回值

Col_c2 VARCHAR2(4000);

BEGIN

FOR cur IN (SELECT c2 FROM t WHERE c1=tmp_c1) LOOP

Col_c2 := Col_c2||cur.c2;

END LOOP;

Col_c2 := rtrim(Col_c2,1);

RETURN Col_c2;

oracle行列转换

select id,max(姓名) 姓名,max(身份证号) 身份证号,max(性别) 性别,max(出生日期) 出生日期,max(年龄) 年龄 from

(select

id,

case when name='姓名' then text end 姓名,

case when name='身份证号' then text end 身份证号,

case when name='性别' then text end 性别,

case when name='出生日期' then text end 出生日期,

case when name='年龄' then text end 年龄 from 表)

group by id

oracle查询结果怎么行列转换

看例子

SQL select * from test;

CTIME A B C

-------------- ---------- ---------- ----------

02-4月 -15 1 2

SQL select * from test unpivot(value for profile in (A,B,C));

CTIME P VALUE

-------------- - ----------

02-4月 -15 A 1

02-4月 -15 B 2

oracle中怎么将列转换为行

可以使用wm_concat()函数;

下面是我做的一个例子,可以参考下,当然具体语法可以百度,也可以去官方文档查:

SCOTT@ ysdb1show user

USER is "SCOTT"

SCOTT@ ysdb1create table test_concat(id number(5),name varchar2(10));

Table created.

SCOTT@ ysdb1insert into test_concat values(1,'a');

1 row created.

SCOTT@ ysdb1insert into test_concat values(1,'b');

1 row created.

SCOTT@ ysdb1insert into test_concat values(1,'c');

1 row created.

SCOTT@ ysdb1insert into test_concat values(2,'q');

1 row created.

SCOTT@ ysdb1insert into test_concat values(2,'w');

1 row created.

SCOTT@ ysdb1insert into test_concat values(2,'e');

1 row created.

SCOTT@ ysdb1insert into test_concat values(2,'f');

1 row created.

SCOTT@ ysdb1select * from test_concat;

ID NAME

---------- ----------

1 a

1 b

1 c

2 q

2 w

2 e

2 f

7 rows selected.

SCOTT@ ysdb1select wm_concat(name) from test_concat;

WM_CONCAT(NAME)

--------------------------------------------------------------------------------

a,b,c,q,w,e,f

SCOTT@ ysdb1select id,wm_concat(name) from test_concat group by id;

ID WM_CONCAT(NAME)

---------- --------------------------------------------------------------------------------

1 a,c,b

2 q,f,e,w


当前标题:oracle行列怎么转换,Oracle行列转换
网站链接:http://scyingshan.cn/article/phcpcd.html