oracle中一个表的行列转换
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了oracle中一个表的行列转换相关的知识,希望对你有一定的参考价值。
一个查询出来的结果,有3列N行,行不固定,怎么用一个动态的语句实现行列转换,我用的PL/SQL,oracle10g数据库。
网上找了个方法
declare @sql varchar ( 8000 )
set @sql = ' select Name as ' + ' 姓名 '
select @sql = @sql + ' , max(case Subject when ''' + Subject + ''' then Result else 0 end) [ ' + Subject + ' ] '
from ( select distinct Subject from tb) as a
set @sql = @sql + ' from tb group by name '
exec ( @sql )
plsql不能执行,谁知道该怎么做
由于结果集不固定,所以需要一个动态的语句来完成转换,谁能帮我把它转换成oracle的存储过程也可以
SELECT DEPTNO AS "部门编号",
DNAME AS "部门名称",
SUM(F1) AS "文员总工资",
SUM(F2) AS "销售总工资",
SUM(F3) AS "经理总工资",
SUM(F4) AS "主管总工资",
SUM(F5) AS "分析师总工资"
FROM
(select E.DEPTNO,D.DNAME,
case when job = 'CLERK' THEN SAL ELSE 0 END F1,
case when job = 'SALESMAN' then SAL else 0 end f2,
case when job = 'MANAGER' then SAL else 0 end f3,
case when job = 'PRESIDENT' then SAL else 0 end f4,
case when job = 'ANALYST' then SAL else 0 end f5
from emp E,DEPT D
WHERE E.DEPTNO = D.DEPTNO
)
GROUP BY DEPTNO,DNAME追问
用这个方法我至少得写2000多个case
追答可以应对行固定的情况。
如果要写2000多个case,那就说明行是不固定的,或者选的转换字段不对。
原因和简单,比如我有一个表记录为
1 x 2011-11-11
2 y 2011-11-12
转换完应该是把这两行竖起来,
但是你要这每一列的类型都是什么呢???
无法确定,如果你都给定义成nvarchar类型,那长度又是多少?
如果给最大长度,那么字段个数是多少?
如果是根据行数计算,那么表中有百万条记录呢?这拥有百万列的表可能存在吗?
所以说,这东西不可能存在完全通用的,限制条件太多。
只能用固定的或者固定限制的。 参考技术C select WMSYS.WM_CONCAT(col_name) from dual
oralce 9以上这个可以用.
不过大小有限制.自己试试看吧. 参考技术D 你这是个 SQL_Server的存储过程啊!
在Pl/sql里面肯定不行啊!追问
弄到plsql里面该怎么做呢
追答我给你我以前写的示例 你可以看看 照样子做一个
这个语句只是查询一个系统表 不会对系统表进行任何操作
可以放心使用
SELECT SUM(DECODE(v.NAME,'session logical reads',v.VALUE,0)) AS session_logical_reads,
SUM(DECODE(v.NAME,'physical reads',v.VALUE,0)) AS physical_reads,
SUM(DECODE(v.NAME,'physical reads direct',v.VALUE,0)) AS physical_reads_direct,
SUM(DECODE(v.NAME,'physical reads direct (lob)',v.VALUE,0)) AS physical_reads_direct_lob,
SUM(DECODE(v.NAME,'db block gets',v.VALUE,0)) AS db_block_gets,
SUM(DECODE(v.NAME,'consistent gets',v.VALUE,0)) AS consistent_gets
FROM
(SELECT NAME, VALUE
FROM V$SYSSTAT
WHERE NAME IN ('session logical reads','physical reads',
'physical reads direct','physical reads direct (lob)','db block gets','consistent gets')) v;
这个方法是静态的吧,我需要一个可以随结果集改变的转换,因为查询出来的结果有上千行,并且条件不一样 ,结果也不固定
追答上千行,你这个需求太夸张了
你查出来,放到excel里面 自己转置吧!
ABAP 内表的行列转换-NEW
REPORT Z_TEST_COL_TO_ROW.
TYPE-POOLS: slis.
TABLES: mseg,mkpf.
DATA: gd_fieldcat TYPE slis_t_fieldcat_alv.
DATA: sla TYPE slis_layout_alv ,
ivariant LIKE disvariant,
i_repid LIKE sy-repid ,
i_excluding TYPE slis_t_extab.
TYPES: BEGIN OF ty_sum ,
werks LIKE mseg-werks ,
matnr LIKE mseg-matnr ,
lgort LIKE mseg-lgort ,
bwart LIKE mseg-bwart ,
menge LIKE mseg-menge ,
END OF ty_sum.
DATA : itab TYPE STANDARD TABLE OF ty_sum WITH HEADER LINE .
DATA : hs_sum TYPE HASHED TABLE OF ty_sum WITH UNIQUE KEY werks matnr lgort bwart WITH HEADER LINE .
DATA : itab_sum TYPE STANDARD TABLE OF ty_sum WITH HEADER LINE .
DATA: BEGIN OF itab_out OCCURS 0,
werks LIKE mseg-werks ,
matnr LIKE mseg-matnr ,
lgort LIKE mseg-lgort ,
menge01 TYPE i ,
menge02 TYPE i ,
menge03 TYPE i ,
menge04 TYPE i ,
menge05 TYPE i ,
menge06 TYPE i ,
menge07 TYPE i ,
menge08 TYPE i ,
menge09 TYPE i ,
menge10 TYPE i ,
menge11 TYPE i ,
menge12 TYPE i ,
menge13 TYPE i ,
menge14 TYPE i ,
menge15 TYPE i ,
menge16 TYPE i ,
menge17 TYPE i ,
menge18 TYPE i ,
menge19 TYPE i ,
menge20 TYPE i ,
menge21 TYPE i ,
menge22 TYPE i ,
menge23 TYPE i ,
menge24 TYPE i ,
menge25 TYPE i ,
menge26 TYPE i ,
line_sum TYPE i ,
END OF itab_out.
TYPES: BEGIN OF ty_bwart ,
bwart LIKE mseg-bwart ,
END OF ty_bwart.
DATA : hs_bwart TYPE HASHED TABLE OF ty_bwart WITH UNIQUE KEY bwart WITH HEADER LINE .
DATA : itab_bwart TYPE STANDARD TABLE OF ty_bwart WITH HEADER LINE .
FIELD-SYMBOLS: <f_fs1> ,
<f_fs2>.
DATA: max_count TYPE i .
SELECT-OPTIONS:
s_mblnr FOR mseg-mblnr ,
s_budat FOR mkpf-budat .
PARAMETERS: p_sum AS CHECKBOX .
START-OF-SELECTION.
PERFORM getdata.
PERFORM fixdata.
IF p_sum = ‘X‘.
PERFORM outdata.
ELSE