sql - Join a subquery of record count with main query - Avoid redundant query -
i have 2 tables, namely sequence table , work table. have join them , fetch p_key , l_key work table each tn_id in sequence table. now, want attach count of {p_key,l_key} each tn_id each tn_id of sequence table. had write join of sequence table , work table 2 times. want avoid that. how can achieve this?
select distinct a.tm_id, rtrim(a.tn_id) tn_id, w.p_key, w.l_key sequence_table left outer join work_table w on a.tm_id=w.tm_id , rtrim(a.tn_id)=rtrim(w.tn_id) join (select a.tm_id tm_id_c, a.tn_id tn_id_c, count(*) count_ sequence_table left outer join work_table w on a.tm_id=w.tm_id , rtrim (a.tn_id) = rtrim (w.tn_id) group a.tm_id, a.tn_id) c on a.tm_id = c.tm_id_c , a.tn_id = c.tn_id_c
ps: solutions oracle sql , not pl/sql prefered.
use with
clause factor subquery, see with clause : subquery factoring
using more readable , in cases faster queries.
Comments
Post a Comment