2011-07-13 19 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