2011-07-14 44 views
0
with one as 
    (Select sno = ROW_NUMBER()OVER (order by complaint_id), Complaint_Id, Complaint.ComplaintType_id, Complaint.complaintProfileId, Complaint.Description, 
           Complaint.Email, Complaint.PriorityLevel_id, Complaint.Date_Complained, Complaint.Status, Complaint.AdminComments, Complaint.Phone, Complaint.Evidence 
        from Complaints Complaint) 

此查詢的結果是(不是整個結果)內連接兩個表,與第

sno complaintProfileId 
1    14 
2    15 
3    15 
4    14 
5    14 
6    13 

第二子查詢:

two as 
        (SELECT Complaint.complaintProfileId, 
      CASE 
       WHEN MMB_Name IS NOT NULL THEN MMB_Name 
       WHEN UPPMembership.profile_id IS NOT NULL THEN 'UPP' 
       ELSE 'Not found' 
      END as Name 
     FROM Complaints Complaint 
     LEFT JOIN MMBMembership 
     ON MMBMembership.profile_id = Complaint.complaintProfileId 
     left JOIN MMB_BusinessProfiles mmbProfiles 
     ON mmbProfiles.MMB_id = MMBMembership.MMB_id 
     LEFT JOIN UPPMembership 
     ON UPPMembership.profile_id = Complaint.complaintProfileId) 


complaintProfileId Name 
14     UPP 
15    Marlon 
15    Marlon 
14     UPP 
14     UPP 
13     Rodolfo 

所以這是我在哪裏遇到問題

select one.*, two.Name 
from one join two 
on one.complaintProfileId = two.complaintProfileId 

This query returns 36 records. id 14返回9times和15-6times等..

我做一個內部聯接,但仍不能確定

感謝 孫

回答

1

你需要加入一個獨特的密鑰。左側的每個「14」都連接到右側的三個「14」中的每一個。 (3x3 = 9)

+0

好的,謝謝你,我用SNo加入 – Sun

+0

你可能*已經在'two'裏擺脫了Complaint的連接。 –