create or replace procedure p_list_loc is
v_sql varchar2(32000) := '';
begin
for c in (select distinct loc from test order by loc) loop
v_sql := v_sql || '''' ||c.loc|| ''' '||c.loc||',';
end loop;
v_sql := 'create or replace view v_list_loc as '
||'select * from (select item, loc, stock from test) pivot (sum(stock) '
||'for (loc) in ('||rtrim(v_sql, ',')||'))';
execute immediate v_sql;
end p_list_loc;
SELECT *
FROM (SELECT ITEM ,LOC ,STOCK
FROM TABLE_NAME)
PIVOT (SUM(quantity) AS sum_quantity FOR (ITEM) IN (SELECT DISNTINCT(LOC) FROM TABLE_NAME))
ORDER BY ITEM;
3条答案
按热度按时间tkclm6bt1#
请尝试此查询。
问候。
tez616oj2#
对于动态生成的结果,您需要一些动态PLSQL解决方案,类似于以下创建视图
v_list_loc
的过程:在过程代码中,将
test
替换为表名。编译此过程,运行并从生成的视图v_list_loc
中选择结果:每当
loc
列中出现新值时,您需要在从视图中选择之前执行过程。8iwquhpp3#
尝试修改SQL,如下所示: