gpt4 book ai didi

sql - oracle sql 中的动态数据透视 - 过程

转载 作者:行者123 更新时间:2023-12-02 20:51:43 25 4
gpt4 key购买 nike

我从此得到了动态枢轴的解决方案post 。现在我想在 oracle 程序中实现以下语句。

clear columns
COLUMN temp_in_statement new_value str_in_statement
SELECT DISTINCT
LISTAGG('''' || myLetter || ''' AS ' || myLetter,',')
WITHIN GROUP (ORDER BY myLetter) AS temp_in_statement
FROM (SELECT DISTINCT myLetter FROM myTable);
SELECT * FROM
(SELECT myNumber, myLetter, myValue FROM myTable)
PIVOT (Sum(myValue) AS val FOR myLetter IN (&str_in_statement));

提前致谢。

最佳答案

如果您有一个表,希望在每次调用存储过程时插入数据透视表结果,则可以使用以下命令:

CREATE OR REPLACE PROCEDURE dynamic_pivot
AS
v_sql LONG := NULL;
v_statement LONG := NULL;
BEGIN
SELECT DISTINCT LISTAGG(''''
|| myLetter
|| ''' AS '
|| myLetter,',') WITHIN GROUP (
ORDER BY myLetter) AS temp_in_statement
INTO v_statement
FROM
(SELECT DISTINCT myLetter FROM test_data
);
v_sql := 'insert into pivot_table
select * from (SELECT myNumber, myLetter, myValue FROM test_data
)
PIVOT
(
SUM(myValue) AS val FOR myLetter IN (' || v_statement || ')
)';
EXECUTE immediate v_sql;
END;

关于sql - oracle sql 中的动态数据透视 - 过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42009404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com