同一桌上有多个连接?

时间:2015-03-16 09:25:59

标签: sql sql-server join sql-server-2005

我有两张桌子:

tbl_EmploymentSegmentEM:

╔══════╦═════════════╦════════════╦═══════════════╦═══════════════════════════════╦════════════╦═════════════╦══════════════════════════╦════════════════╗
║ SrNo ║ CIBILTuefID ║ Prospectno ║ ApplicantType ║         ApplicantName         ║ SegmentTag ║ AccountType ║ DateReportedandCertified ║ OccupationCode ║
╠══════╬═════════════╬════════════╬═══════════════╬═══════════════════════════════╬════════════╬═════════════╬══════════════════════════╬════════════════╣
║    1 ║           1 ║     718580 ║ APPLICANT     ║ RAJKUMAR GIRISHCHANDRA PANDEY ║ E01        ║          10 ║                 31122014 ║             02 ║
║    2 ║           4 ║     718638 ║ APPLICANT     ║ Anil Kumar Aggarwal           ║ E01        ║          10 ║                 31122014 ║             01 ║
╚══════╩═════════════╩════════════╩═══════════════╩═══════════════════════════════╩════════════╩═════════════╩══════════════════════════╩════════════════╝

tbl_CIBILFieldDescription:

╔════════╦══════════╦══════════════════════════════╦═══════╦═════════════════════════════╗
║ Header ║ FieldTag ║          FieldName           ║ Value ║      ValueDescription       ║
╠════════╬══════════╬══════════════════════════════╬═══════╬═════════════════════════════╣
║ PT     ║       03 ║ TelephoneType                ║ 03    ║ Office Phone                ║
║ EM     ║       03 ║ OccupationCode               ║ 01    ║ Salaried                    ║
║ EM     ║       03 ║ OccupationCode               ║ 02    ║ Self Employed Professional. ║
║ EM     ║       03 ║ OccupationCode               ║ 03    ║ Self Employed               ║
║ EM     ║       03 ║ OccupationCode               ║ 04    ║ Others                      ║
║ EM     ║       05 ║ NetGrossIncomeIndicator      ║ G     ║ Gross Income                ║
║ EM     ║       05 ║ NetGrossIncomeIndicator      ║ N     ║ Net Income                  ║
║ EM     ║       06 ║ MonthlyAnnualIncomeIndicator ║ M     ║ Net Monthly                 ║
║ EM     ║       06 ║ MonthlyAnnualIncomeIndicator ║ A     ║ Net Annual                  ║
║ SC     ║       01 ║ ScoreCardName                ║ 01    ║ CIBILTUSCR                  ║
╚════════╩══════════╩══════════════════════════════╩═══════╩═════════════════════════════╝

我正在尝试从tbl_CIBILFieldDescription获取相应值的帐户类型和职业代码说明。

我试过了:

SELECT DISTINCT CIBILTuefID, 
                Prospectno, 
                ApplicantType, 
                ApplicantName, 
                SegmentTag, 
                AccountType, 
                DateReportedandCertified, 
                OccupationCode, 
                mst.ValueDescription  AS OccupationCodeDesc, 
                Income, 
                NetGrossIncomeIndicator, 
                mst.ValueDescription AS NetGrossIncomeIndicatorDesc, 
                MonthlyAnnualIncomeIndicator, 
                DateofEntryforErrorCode, 
                ErrorCode, 
                DateofEntryforCIBILRemarksCode, 
                CIBILRemarksCode, 
                DateofEntryforErrorDisputeRemarksCode, 
                ErrorDisputeRemarksCode1, 
                ErrorDisputeRemarksCode2, 
                MkrId, 
                MkdDt 
FROM   tbl_EmploymentSegmentEM EM 
INNER JOIN tbl_CIBILFieldDescription mst 
    ON 1 = 1 
WHERE mst.Header = 'EM' 
  AND mst.FieldName = 'OccupationCode' 
  AND mst.Value = EM.OccupationCode 

它似乎适用于OccupationCode,但如果我想同一个查询中的OccupationCodeAccountType怎么办?做这个的最好方式是什么?

1 个答案:

答案 0 :(得分:0)

您可以使用LEFT JOIN多次加入您的表格:

SELECT DISTINCT cibiltuefid, 
                prospectno, 
                applicanttype, 
                applicantname, 
                segmenttag, 
                accounttype, 
                datereportedandcertified, 
                occupationcode, 
                mst.valuedescription  AS OccupationCodeDesc, 
                income, 
                netgrossincomeindicator, 
                mst1.valuedescription AS NetGrossIncomeIndicatorDesc, 
                monthlyannualincomeindicator, 
                dateofentryforerrorcode, 
                errorcode, 
                dateofentryforcibilremarkscode, 
                cibilremarkscode, 
                dateofentryforerrordisputeremarkscode, 
                errordisputeremarkscode1, 
                errordisputeremarkscode2, 
                mkrid, 
                mkddt 
                ,mst.ValueDescription AS Occupation
                ,mst1.ValueDescription AS Account
FROM tbl_employmentsegmentem EM
LEFT JOIN tbl_cibilfielddescription mst 
    ON mst.fieldname = 'OccupationCode' 
    AND mst.value = EM.occupationcode 
    AND mst.header = 'EM'
LEFT JOIN tbl_cibilfielddescription mst1 
    ON mst1.fieldname = 'AccountType' 
    AND mst1.value = EM.accounttype
    AND mst1.header = 'EM'