sql - UNION or Left Join? -
i have try union 2 tables , left join result no want.any idea ?please help.
table a
deduction_no group_id emp_id service_code amt prd533 prg158 null 2351 35 prd533 prg158 null 4854 160 prd533 prg158 null 4857 44 prd533 prg158 null 6611 3
table b
deduction_no group_id emp_id is_selected deduct_amt prd533 prg158 001 y 82 prd533 prg158 001 n 0 prd533 prg158 002 y 195 prd533 prg158 002 n 0 prd533 prg158 003 y 79 prd533 prg158 003 n 0 prd533 prg158 004 y 107 prd533 prg158 004 n 0
output,but don't want (union)
deduction_no group_id emp_id service_code deduct_atm prd533 prg158 001 - prd533 prg158 002 - prd533 prg158 003 - prd533 prg158 004 - prd533 prg158 - 2351 35 prd533 prg158 - 4854 160 prd533 prg158 - 4857 44 prd533 prg158 - 6611 3
output using left join
deduction_no group_id emp_id service_code deduct_atm prd533 prg158 001 2351 35 prd533 prg158 001 4854 160 prd533 prg158 001 4857 44 prd533 prg158 001 6611 3 prd533 prg158 002 2351 35 prd533 prg158 002 4854 160 prd533 prg158 002 4857 44 prd533 prg158 002 6611 3
desired output
deduction_no group_id emp_id service_code amt prd533 prg158 001 2351 35 prd533 prg158 002 2351 35 prd533 prg158 003 2351 35 prd533 prg158 001 4854 160 prd533 prg158 002 4857 44 prd533 prg158 004 4857 44 prd533 prg158 003 6611 3 prd533 prg158 004 6611 3
thank you
this query:
select a.deduction_no, a.group_id, b.emp_id, a.service_code, a.amt #a left join #b b on a.deduction_no = b.deduction_no b.is_selected ='y' order service_code
will give following output:
deduction_no group_id emp_id service_code amt prd533 prg158 1 2351 35 prd533 prg158 2 2351 35 prd533 prg158 3 2351 35 prd533 prg158 4 2351 35 prd533 prg158 1 4854 160 prd533 prg158 2 4854 160 prd533 prg158 3 4854 160 prd533 prg158 4 4854 160 prd533 prg158 1 4857 44 prd533 prg158 2 4857 44 prd533 prg158 3 4857 44 prd533 prg158 4 4857 44 prd533 prg158 1 6611 3 prd533 prg158 2 6611 3 prd533 prg158 3 6611 3 prd533 prg158 4 6611 3
since don't give me more information can't you, should provide how data should selected, on conditions , etc.
Comments
Post a Comment