sql - Display all rows of data from both tables even though conditions exist -
i have sql query 2 tables need in expanding results.
the query running is:
select b.obj_full, b.subj_full, b.act_ytd_ty, sum(a.jnl_value) jnl_total db2adm2.jnlfile inner join db2adm2.tfincatp b on b.obj_full = a.jnl_obj , b.subj_full = a.jnl_subj a.jnl_year ='15' , a.jnl_processed ='n' , obj_full = 'tbbbb' group b.obj_full, b.subj_full, b.act_ytd_ty
the data being returned is:
obj_full subj_full act_ytd_ty jnl_total ----------------------------------------- tbbbb 9404 -9666.73 -547.78 tbbbb 9405 -13098.05 -24.39
i have split query above 2 separate queries show data being returned each one.
query 1 - journal values not processed week on code tbbbb
select jnl_obj, jnl_subj, sum(jnl_value) jnl_value db2adm2.jnlfile jnl_year ='15' , jnl_processed ='n' , jnl_obj = 'tbbbb' group jnl_obj, jnl_subj
results on query 1
jnl_obj jnl_subj jnl_value --------------------------- tbbbb 9404 -547.78 tbbbb 9405 -24.39
query 2 - year date values on code tbbbb
select obj_full, subj_full, act_ytd_ty db2adm2.tfincatp obj_full = 'tbbbb'
results on query 2
obj_full subj_full act_ytd_ty ------------------------------- tbbbb 3420 12000 tbbbb 3473 18453.02 tbbbb 3524 2480.4 tbbbb 3704 585 tbbbb 6812 0 tbbbb 6910 0 tbbbb 7704 -4142.71 tbbbb 8199 -25 tbbbb 9400 -168363.1 tbbbb 9403 -457.56 tbbbb 9404 -9666.73 tbbbb 9405 -13098.05 tbbbb 9812 0
the result looking query 2 additional column query 1 being jnl_value, every tbbbb code not have journal each week, rows journal being returned, column must insert 0 if present value not there.
any ideas how please?
if want 0 when there no record matching b one:
select b.obj_full, b.subj_full, b.act_ytd_ty, isnull(a.jnl_value, 0) jnl_value db2adm2.jnlfile right outer join db2adm2.tfincatp b on b.obj_full = a.jnl_obj , b.subj_full = a.jnl_subj a.jnl_year ='15' , a.jnl_processed ='n' , obj_full = 'tbbbb'
(do search on outer joins: there left, right, , full outer joins.) since want every b row, i've removed group by. , put in right outer join b's when there no matching a.
Comments
Post a Comment