首先 - 我建议使用不同的查询方法
select * from (
select distinct ref,
sum(val) over(partition by ref) / sum(val) over() val
from ref_table r
join transaction_table t
using(id)
)
pivot (min(val) for ref in ('Red', 'Blue', 'Green'))
其次,下面是根据 ref_table 内容生成这个的方法
select '''
select * from (
select distinct ref,
sum(val) over(partition by ref) / sum(val) over() val
from ref_table r
join transaction_table t
using(id)
)
pivot (min(val) for ref in (''' || (select string_agg(distinct '"' || ref || '"') from ref_table) || '''))
'''
最后,您可以像下面的示例一样执行它
execute immediate '''
select * from (
select distinct ref,
sum(val) over(partition by ref) / sum(val) over() val
from ref_table r
join transaction_table t
using(id)
)
pivot (min(val) for ref in (''' || (select string_agg(distinct '"' || ref || '"') from ref_table) || '''))
'''