最近在做报表导出功能,遇到需要行转列的功能,sql如下
select nvl(t1.credit_code,'空') as "监管条件1", nvl(t2.credit_code,'空') as "监管条件2",
case when a.is_using = 0 then '停用' else '启用' end as "商品状态",
nvl(to_char(a.create_time,'yyyy-mm-dd'),'空') as "创建日期",
nvl(yua.user_name,'空') as "创建人",
nvl(to_char(a.disabled_date,'yyyy-mm-dd'),'空') as "最新失效日期",
nvl(yud.user_name,'空') as "最新失效人",
nvl(a.apply_remark,'空') as "备注"
from info a
left join (
select info_id, wm_concat(to_char(credit_code)) as credit_code
from sup
where condition ='10' and org_id ='123456'
group by info_id
) t1 on a.goods_id = t1.info_id
left join (
select info_id, wm_concat(to_char(credit_code)) as credit_code
from ybg_goods_sup
where condition ='20' and org_id ='123456'
group by info_id
) t2 on a.goods_id = t2.info_id
inner join user yua on yua.id = a.creator
left join user yud on yud.id = a.disabled_by
left join user yuc on yuc.id = a.classif_by
where a.isDelete='0' and a.typeFlag ='1' and org_id ='123456'
8000多条数据整个查询起来用时接近两分钟,这领导肯定会炸起来。。。。
没办法,只能自己默默优化,就一点点把sql进行分解(查询部分有clob类型判断,之前分解方向有误)最终定位到是行转列查询的问题,后面用listagg(credit_code,',') within group(order by null)代替wm_concat函数,查询速度由之前的两分钟优化到3秒多。
目前正在了解listagg(字段名称,',') within group(order by null)的用法,第一次写,欢迎写评论讨论技术。