2014-07-17 82 views
1

我從這個查詢中獲取許多重複的行。我的左連接有問題嗎?我已經試過內部人員也加入,但他們根本沒有幫助。我認爲這個問題是什麼,如果一個左連接是真的,那麼它增加了一個排的結果多個SQL連接後重復的行

SELECT DP.FirstName, DP.LastName, DECA.Email, UI.Id, ET.EmplId, 
HPPTR.OrganizationUnitId, A.CountryRegionId, ET.EmplIdentNumber, 
ET.JobId_INN, HPPTR.Description, ET.HRMResponsible_INN, A.Street, A.ZipCode, 
A.City, HPER.ValidFromDateTime, HCJF.INN_JobAgreementType, 
HPJTR.INN_EmpJobType, HPPTR.hrmemploymentfactor 

FROM DirPartyTable AS DP 

LEFT JOIN DIRPARTYECOMMUNICATIONRELA2608 AS DPECR ON DP.PartyId = DPECR.PartyId 
LEFT JOIN DirECommunicationAddress AS DECA ON DPECR.ValuesRecId = DECA.RecId 

LEFT JOIN EmplTable AS ET ON DP.PartyId = ET.PartyId 
LEFT JOIN SYSCOMPANYUSERINFO AS SCUI ON ET.EmplId = SCUI.EmplId 
LEFT JOIN USERINFO AS UI ON SCUI.UserId = UI.Id 

LEFT JOIN HRPPARTYPOSITIONTABLERELAT2226 AS HPPTR ON ET.EmplId = HPPTR.Reference 
LEFT JOIN HRPPartyJobTableRelationship AS HPJTR ON HPPTR.JobId = HPJTR.Jobid 
LEFT JOIN HRMCompJobFunction AS HCJF ON HPJTR.HrmCompJobFunctionId = HCJF.JobFunctionId 

LEFT JOIN Address AS A ON ET.RecId = A.AddrRecId 

LEFT JOIN HRMPARTYEMPLOYEERELATIONSHIP AS HPER ON ET.EmplId = HPER.EmplId 

WHERE DP.DataAreaId != 'cvc' 
AND DECA.ECommunicationTypeId = 'email' 
AND A.addrtableid='256' 
AND UI.NETWORKALIAS IS NOT NULL 
AND A.DataAreaId = DP.DataAreaId 
AND SCUI.DataAreaId = DP.DataAreaId 
AND ET.DataAreaId = DP.DataAreaId 

ORDER BY ET.EmplId 

這裏是架構

FirstName LastName Email Id EmplId OrganizationUnitId CountryRegionId EmplIdentNumber CountryRegionId Description  Street ZipCode City ValidFromDateTime INN_JobAgreementType INN_EmpJobType hrmemploymentfactor 
John Head [email protected] johnh headjh org1 FI 131564-234523 CHZ IT GUY  rockstreet 534523 Bigplace 2009-02-16 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org2 FI 131564-234524 CHZ IT GUY  rockstreet 534523 Bigplace 2009-02-16 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org3 FI 131564-234525 CHZ IT GUY  rockstreet 534523 Bigplace 2009-02-16 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org4 FI 131564-234526 CHZ IT GUY  rockstreet 534523 Bigplace 2009-02-16 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org5 FI 131564-234527 CHZ IT GUY  rockstreet 534523 Bigplace 2009-02-16 00:00:00.000 NULL  0.000000000000 
John Head [email protected] johnh headjh org1 FI 131564-234528 CHZ IT GUY  rockstreet 534523 Bigplace 2009-02-16 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org2 FI 131564-234529 CHZ IT GUY  rockstreet 534523 Bigplace 2009-02-16 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org3 FI 131564-234530 CHZ IT GUY  rockstreet 534523 Bigplace 2009-02-16 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org4 FI 131564-234531 CHZ IT GUY  rockstreet 534523 Bigplace 2009-02-16 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org5 FI 131564-234532 CHZ IT GUY  rockstreet 534523 Bigplace 2009-02-16 00:00:00.000 NULL  0.000000000000 
John Head [email protected] johnh headjh org1 FI 131564-234533 CHZ IT GUY  rockstreet 534523 Bigplace 2009-01-19 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org2 FI 131564-234534 CHZ IT GUY  rockstreet 534523 Bigplace 2009-01-19 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org3 FI 131564-234535 CHZ IT GUY  rockstreet 534523 Bigplace 2009-01-19 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org4 FI 131564-234536 CHZ IT GUY  rockstreet 534523 Bigplace 2009-01-19 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org5 FI 131564-234537 CHZ IT GUY  rockstreet 534523 Bigplace 2009-01-19 00:00:00.000 NULL  0.000000000000 
John Head [email protected] johnh headjh org1 FI 131564-234538 CHZ IT GUY  rockstreet 534523 Bigplace 2009-01-19 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org2 FI 131564-234539 CHZ IT GUY  rockstreet 534523 Bigplace 2009-01-19 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org3 FI 131564-234540 CHZ IT GUY  rockstreet 534523 Bigplace 2009-01-19 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org4 FI 131564-234541 CHZ IT GUY  rockstreet 534523 Bigplace 2009-01-19 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org5 FI 131564-234542 CHZ IT GUY  rockstreet 534523 Bigplace 2009-01-19 00:00:00.000 NULL  0.000000000000 
John Head [email protected] johnh headjh org1 FI 131564-234543 CHZ IT GUY  rockstreet 534523 Bigplace 2011-01-01 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org2 FI 131564-234544 CHZ IT GUY  rockstreet 534523 Bigplace 2011-01-01 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org3 FI 131564-234545 CHZ IT GUY  rockstreet 534523 Bigplace 2011-01-01 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org4 FI 131564-234546 CHZ IT GUY  rockstreet 534523 Bigplace 2011-01-01 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org5 FI 131564-234547 CHZ IT GUY  rockstreet 534523 Bigplace 2011-01-01 00:00:00.000 NULL  0.000000000000 
John Head [email protected] johnh headjh org1 FI 131564-234548 CHZ IT GUY  rockstreet 534523 Bigplace 2011-01-01 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org2 FI 131564-234549 CHZ IT GUY  rockstreet 534523 Bigplace 2011-01-01 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org3 FI 131564-234550 CHZ IT GUY  rockstreet 534523 Bigplace 2011-01-01 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org4 FI 131564-234551 CHZ IT GUY  rockstreet 534523 Bigplace 2011-01-01 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org5 FI 131564-234552 CHZ IT GUY  rockstreet 534523 Bigplace 2011-01-01 00:00:00.000 NULL  0.000000000000 
John Head [email protected] johnh headjh org1 FI 131564-234553 CHZ IT GUY  rockstreet 534523 Bigplace 2011-01-02 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org2 FI 131564-234554 CHZ IT GUY  rockstreet 534523 Bigplace 2011-01-02 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org3 FI 131564-234555 CHZ IT GUY  rockstreet 534523 Bigplace 2011-01-02 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org4 FI 131564-234556 CHZ IT GUY  rockstreet 534523 Bigplace 2011-01-02 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org5 FI 131564-234557 CHZ IT GUY  rockstreet 534523 Bigplace 2011-01-02 00:00:00.000 NULL  0.000000000000 
John Head [email protected] johnh headjh org1 FI 131564-234558 CHZ IT GUY  rockstreet 534523 Bigplace 2011-01-02 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org2 FI 131564-234559 CHZ IT GUY  rockstreet 534523 Bigplace 2011-01-02 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org3 FI 131564-234560 CHZ IT GUY  rockstreet 534523 Bigplace 2011-01-02 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org4 FI 131564-234561 CHZ IT GUY  rockstreet 534523 Bigplace 2011-01-02 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org5 FI 131564-234562 CHZ IT GUY  rockstreet 534523 Bigplace 2011-01-02 00:00:00.000 NULL  0.000000000000 
John Head [email protected] johnh headjh org1 FI 131564-234563 CHZ IT GUY  rockstreet 534523 Bigplace 2010-02-04 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org2 FI 131564-234564 CHZ IT GUY  rockstreet 534523 Bigplace 2010-02-04 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org3 FI 131564-234565 CHZ IT GUY  rockstreet 534523 Bigplace 2010-02-04 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org4 FI 131564-234566 CHZ IT GUY  rockstreet 534523 Bigplace 2010-02-04 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org5 FI 131564-234567 CHZ IT GUY  rockstreet 534523 Bigplace 2010-02-04 00:00:00.000 NULL  0.000000000000 
John Head [email protected] johnh headjh org1 FI 131564-234568 CHZ IT GUY  rockstreet 534523 Bigplace 2010-02-04 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org2 FI 131564-234569 CHZ IT GUY  rockstreet 534523 Bigplace 2010-02-04 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org3 FI 131564-234570 CHZ IT GUY  rockstreet 534523 Bigplace 2010-02-04 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org4 FI 131564-234571 CHZ IT GUY  rockstreet 534523 Bigplace 2010-02-04 00:00:00.000 NULL  1.000000000000 
John Head [email protected] johnh headjh org5 FI 131564-234572 CHZ IT GUY  rockstreet 534523 Bigplace 2010-02-04 00:00:00.000 NULL  0.000000000000 
+0

小心分享您的表格架構? –

回答

3

你需要考慮是否在「正確」的列是獨一無二的。

例如:在

FROM DirPartyTable AS DP 
LEFT JOIN DIRPARTYECOMMUNICATIONRELA2608 AS DPECR ON DP.PartyId = DPECR.PartyId 

DPECR.PartyID獨特之處?如果不是,則映射到DPECR中多行的DP中的每一行都將出現多次。

...並繼續爲所有其他聯接。語法上,他們很好,但你需要問自己的獨特性。

要弄清楚發生了什麼,請嘗試執行SELECT 1而不是您的字段列表,並從第一個表格開始,查看返回的行數。然後嘗試使用前兩個或三個表,直到找出哪些連接使返回的行數增加。那些是你的問題,並需要進一步調查你的餐桌設計。

+0

你的意思是說在ON-word之後我把表格放在哪個順序上?例如,它是不同的,如果我寫DP.PartyId = DPECR.PartyId或DPECR.PartyId = DP.PartyId –

+0

編號。這不會影響它。如果您加入的列之間有多個行匹配,您將獲得每個組合。 –

+0

好的thx和你選擇1的含義是什麼?你能舉例說明我如何開始調查這些聯結嗎? –