SQL如何加入两个表

时间:2015-07-10 06:12:45

标签: sql

表a使用下面的代码与另外两个表连接。

代码

 SELECT DISTINCT
 PY.DEDUCTION_VOUCHER_NO,
 PY.DEDUCTION_PER_PAYRUN,
 COALESCE (SV.EMPLOYEE_CODE,MEM.EMPLOYEE_CODE) AS EMPLOYEE_CODE,
 SV.HOME_AMT_AFTER_TAX,
 SV.PIECE_RATE_GROUP_CODE, 
 SV.SERVICE_CODE,
 SV.SUBJECT_TYPE
 FROM PY_DEDUCTION PY
 LEFT OUTER JOIN PY_DEDUCTION_SERVICE SV ON (PY.DEDUCTION_VOUCHER_NO = SV.DEDUCTION_VOUCHER_NO)
 LEFT OUTER JOIN PY_DEDUCTION_SV_GRP_MEMBER MEM ON (PY.DEDUCTION_VOUCHER_NO = MEM.DEDUCTION_VOUCHER_NO)

Display Table a
SUBJECT_TYPE   PIECE_RATE_GROUP_CODE Employee_code DEDUCTION_VOUCHER_NO  SERVICE_CODE   HOME_AMT_AFTER_TAX   DEDUCTION_PER_PAYRUN
EMP                                     001          D2001               AC01            $50                        $40
GRP                GP1005               001          D2001               AC02            $100                       $80 
GRP                GP1005               002          D2001               AC02            $70                        $20
EMP                                     003          D2233               AC05            $100                       $100

现在需要加入表b。但有些Deduction_Voucher_No不包括在表b中

Table b
Payslip_Voucher_NO   Deduction_Voucher_No Employee_code OutStanding_Amount
P1001                 D2001                 001          $10
P1002                 D2001                 001          $20
P1003                 D2001                 002          $50

我尝试应用左外连接,

 SELECT DISTINCT
 PY.DEDUCTION_VOUCHER_NO,
 PY.DEDUCTION_PER_PAYRUN,
 COALESCE (SV.EMPLOYEE_CODE,MEM.EMPLOYEE_CODE) AS EMPLOYEE_CODE,
 SV.HOME_AMT_AFTER_TAX,
 SV.PIECE_RATE_GROUP_CODE, 
 SV.SERVICE_CODE,
 SV.SUBJECT_TYPE,
 tbl.Payslip_Voucher_No,
 tbl.Outstanding_Amount
 FROM PY_DEDUCTION PY
 LEFT OUTER JOIN PY_DEDUCTION_SERVICE SV ON (PY.DEDUCTION_VOUCHER_NO = SV.DEDUCTION_VOUCHER_NO)
 LEFT OUTER JOIN PY_DEDUCTION_SV_GRP_MEMBER MEM ON (PY.DEDUCTION_VOUCHER_NO = MEM.DEDUCTION_VOUCHER_NO)
 LEFT OUTER JOIN table b tlb ON (PY.DEDUCTION_VOUCHER_NO = tbl.DEDUCTION_VOUCHER_NO)

但是显示错误,显示所有在相同DEDUCTION_VOUCHER_NO下的payslip_voucher_no

SUBJECT_TYPE   PIECE_RATE_GROUP_CODE Employee_code DEDUCTION_VOUCHER_NO  Payslip_Voucher_NO  SERVICE_CODE   HOME_AMT_AFTER_TAX   DEDUCTION_PER_PAYRUN  OutStanding_Amount
EMP                                     001          D2001               P1001               AC01            $50                        $40               $10
EMP                                     001          D2001               P1002               AC01            $50                        $40               $20
EMP                                     001          D2001               P1003               AC01            $50                        $40               $50

预期结果应为

SUBJECT_TYPE   PIECE_RATE_GROUP_CODE Employee_code DEDUCTION_VOUCHER_NO  Payslip_Voucher_NO  SERVICE_CODE   HOME_AMT_AFTER_TAX   DEDUCTION_PER_PAYRUN  OutStanding_Amount
EMP                                     001          D2001               P1001               AC01            $50                        $40               $10
GRP                GP1005               001          D2001               P1002               AC02            $100                       $80               $20
GRP                GP1005               002          D2001               P1003               AC02            $70                        $20               $50
EMP                                     003          D2233               empty               AC05            $100                       $100              $0

有什么想法吗?感谢

1 个答案:

答案 0 :(得分:2)

 SELECT DISTINCT
 PY.DEDUCTION_VOUCHER_NO,
 PY.DEDUCTION_PER_PAYRUN,
 COALESCE (SV.EMPLOYEE_CODE,MEM.EMPLOYEE_CODE) AS EMPLOYEE_CODE,
 SV.HOME_AMT_AFTER_TAX,
 SV.PIECE_RATE_GROUP_CODE, 
 SV.SERVICE_CODE,
 SV.SUBJECT_TYPE,
 ISNULL(b.OutStanding_Amount, 0) AS OutStanding_Amount -- Add this line
 FROM PY_DEDUCTION PY
 LEFT OUTER JOIN PY_DEDUCTION_SERVICE SV ON (PY.DEDUCTION_VOUCHER_NO = SV.DEDUCTION_VOUCHER_NO)
 LEFT OUTER JOIN PY_DEDUCTION_SV_GRP_MEMBER MEM ON (PY.DEDUCTION_VOUCHER_NO = MEM.DEDUCTION_VOUCHER_NO)
 -- Add this line, I don't know which table contains Payslip_Voucher_NO to avoid unwanted rows
 LEFT OUTER JOIN TableB b ON 
 (
     PY.DEDUCTION_VOUCHER_NO = b.DEDUCTION_VOUCHER_NO 
     AND ??.Payslip_Voucher_NO = b.Payslip_Voucher_NO
     AND COALESCE (SV.EMPLOYEE_CODE,MEM.EMPLOYEE_CODE) = b.EMPLOYEE_CODE
 )