2011-07-13 67 views
0
if 
@Status = '0' and @Complaint<> '0' 
WITH NewTable 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), 

with two as 
if exists(select profile_id from MMBmembership 
where profile_id = ComplaintProfileId) 
select MMB_Name as @Name from MMB_BusinessProfiles where MMBId= (select MMB_id from MMBMembership 
where profile_id = ComplaintProfileId) 

if exists(select profile_id from UPPMembership 
where profile_id = ComplaintProfileId) 


set @Name = 'UPP' 
else 
set @name = 'Not Found' 



SELECT * FROM NewTable,@Name left outer join two on 
newtable.complaintProfileid = two.ProfileId 
WHERE (ComplaintType_id = @Complaint) ORDER BY Date_Complained desc ,PriorityLevel_id 

Thanks 
Sun 

回答

0

除非我誤解,我不認爲你需要一個SUB查詢。您只需要一個LEFT JOIN,是的,您可以使用CASE語句

SELECT 
     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 UPPMembership 
    ON UPPMembership.profile_id = Complaint.complaintProfileId