我有一个很大的查询(不是我写的,但我正在做一些修改)。让我烦恼的一件事是,我在大约四个地方都有相同的 COALESCE 功能。有没有办法解决这个问题,可能是通过从 DUAL 中选择加入?将其分解出来是否有性能优势?
这是稍微粗略的查询:
select
tbl1.gid
[snip]
,COALESCE(t1_end_dt, t6_actl_end_dt,t6_calc_end_dt) perd_end_dt
,t1_end_dt
FROM tbl1
....
JOIN tbl2 ON (t2_pk = wpck_wrkr_id AND
t2_ind_1 ='Y' AND
COALESCE(t1_end_dt, t6_actl_end_dt, t6_calc_end_dt)
BETWEEN t2_strt_dt AND t2_end_dt)
JOIN tbl3 ON (t3_pk = t2_fk_t3_pk AND
COALESCE(t1_end_dt, t6_actl_end_dt, t6_calc_end_dt)
BETWEEN t3_strt_dt AND t3_end_dt)
LEFT JOIN tbl4 tbl4_a ON (tbl4_a.t4_pk = chkw_wkt_id)
.....
GROUP BY tbl1.gid
.....
,COALESCE(t1_end_dt, t6_actl_end_dt, t6_calc_end_dt)
,COALESCE(tbl4_b.t4_or_id, tbl4_a.t4_or_id, t7_or_id )
,t1_end_dt
ORDER BY perd_end_dt
请注意,我已经通过在 SELECT 中将其命名为 pred_end_dt 并然后在 ORDER BY 中通过该名称引用它来分解出其中一个 COALESCE。