我尝试创建自己的动态数据透视程序,但它不起作用。 我的程序是正确创建的,但是当我想执行它时 - 它不起作用。 首先,我尝试了bluefeet的程序并且它正在工作。链接到这个问题 Dynamically pivoting a table Oracle
我的程序应该计算列p_key中值的出现次数。
也许有人知道为什么我的程序无效。
我桌子的代码
create table aa_tabela (
C_ID number(3,0),
gcm number(1,0),
p_key varchar2(30),
p_count number(6,0));
我插入了几个值
INSERT ALL
INTO aa_tabela (C_ID, gcm, p_key, p_count )
VALUES (1, 7, 'Data Mining', 2)
INTO aa_tabela (C_ID, gcm, p_key, p_count )
VALUES (2, 5, 'Data Mining', 3)
INTO aa_tabela (C_ID, gcm, p_key, p_count )
VALUES (3, 3, 'Reporting', 1)
INTO aa_tabela (C_ID, gcm, p_key, p_count )
VALUES (4, 3, 'Olap', 5)
INTO aa_tabela (C_ID, gcm, p_key, p_count )
VALUES (5, 1, 'Reporting', 4)
SELECT * FROM dual;
我的静态PIVOT表正在运行:
SELECT GCM,
SUM(case when p_key = 'Data Mining' then p_count end) AS Data_Mining,
SUM(case when p_key = 'Reporting' then p_count end) AS Reporting,
SUM(case when p_key = 'Olap' then p_count end) AS OLAP
from aa_tabela
group by gcm
order by gcm desc;
结果:
|GCM | DATA_MINING | REPORTING | OLAP |
| 7 | 2 | | |
| 5 | 3 | | |
| 3 | | 1 | 5 |
| 1 | | 4 | |
我的动态数据透视程序正确创建:
CREATE OR REPLACE procedure test_abc(p_cursor in out sys_refcursor)
AS
sql_query long := 'select gcm ';
BEGIN
for x in (select distinct p_key from aa_tabela order by 1)
loop
sql_query := sql_query ||
' , sum(case when p_key = '''||x.p_key||''' then p_count end) as '||x.p_key;
dbms_output.put_line(sql_query);
end loop;
sql_query := sql_query || ' from aa_tabela group by gcm';
open p_cursor for sql_query;
END;
/ 程序TEST_ABC编译
我按照以下代码执行我的程序:
variable x refcursor
exec test_abc(:x);
print x
错误讯息:
Error starting at line : 215 in command -
exec test_abc(:x)
Error report -
ORA-00923: FROM keyword not found where expected
ORA-06512: at "MAGDA.TEST_ABC", line 16
ORA-06512: at line 1
00923. 00000 - "FROM keyword not found where expected"
答案 0 :(得分:0)
您收到错误,因为其中一个值是“数据挖掘”。在您的程序中,它被翻译为列{i} as Data Mining
等别名。
您需要在第9行中将空格更改为下划线或添加配额标记:
' , sum(case when p_key = '''||x.p_key||''' then p_count end) as "'||x.p_key||'"';