oracle 如何聚合多行函数
在BEA论坛上看一位"专家"写的大作,一条SQL语句是
select r.xm, substr(r.csrq,1,4)||'年'||substr(r.csrq,5,2)||'月'||substr(r.csrq,7,2)||'日' csrq, (select dictvalue from zh_dictvalue where dictcode=xb and dictname='rk_xb') xb, (select dictvalue from zh_dictvalue where dictcode=mz and dictname='rk_mz') mz, (select dictvalue from zh_dictvalue where dictcode=ssxq and dictname='rk_xzqh') ssxq, xz, xp, xz, fwcs from czrk_jbxx r,rk_zpxx p where r.gmsfhm=p.gmsfhm and rownum<2
朋友看了一会,然后问我 (select dictvalue from zh_dictvalue where dictcode=xb and dictname='rk_xb') xb, (select dictvalue from zh_dictvalue where dictcode=mz and dictname='rk_mz') mz, (select dictvalue from zh_dictvalue where dictcode=ssxq and dictname='rk_xzqh') ssxq, 这里如何优化,也就是符合条件的三条记录要合并成一条记录.
其实之前有好多这样的问题,但没有一个好的方案,都是嵌套太多,性能损失很大,把三条记录的结果合并.如果最后的sql语句中的select超过三次,那真的还不如直接这样查询.
不过首先这个方法是错误的,因为这三次都在原表中查询,性能损失很大,其实如果是5条,10条,20条,100
Oracle SQL优化:合并多行数据为多列

本文探讨了如何优化Oracle SQL查询,将多次查询结果合并为一行多列,以提高性能。传统的嵌套子查询方法在多条记录时导致性能下降,而使用分析函数可以实现更高效的解决方案,只需对原表查询一次,然后在内存中合并结果,尤其适用于大量数据的场景。
最低0.47元/天 解锁文章
1052





