【问题标题】:Multiple joins on same table?同一张表上的多个连接?
【发布时间】:2015-05-18 08:40:06
【问题描述】:

我有两张桌子:

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 怎么办?做这个的最好方式是什么?

【问题讨论】:

  • INNER JOIN.... ON 1 = 1 请解释一下这个加入想要达到的目的。
  • 这是一个交叉连接,即类似于具有 WHERE 子句中指定的连接条件的旧式连接...

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


【解决方案1】:

您可以使用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'

【讨论】:

  • 我在做同样的事情,除了左连接而不是内连接。谢谢,将标记答案。
猜你喜欢
  • 2015-07-24
  • 1970-01-01
  • 2016-03-07
  • 2011-10-18
  • 1970-01-01
  • 2013-05-23
  • 2021-01-09
  • 1970-01-01
相关资源
最近更新 更多