2014-12-05 70 views
-7

我想基於列值連接兩個表INNER JOIN基於SQL Server中的列值2008

  • 如果pm.ServiceLevelID值是1或NULL然後內部聯接上u.FacilityId
  • 如果pm.ServiceLevelID值2然後內部聯接上u.FacilityServiceId

pm, u具有這些列這兩個表:

ProviderMessages 
    MessageID, FacilityServiceID, ServiceLevelID, FacilityID, ProviderTypeID 

User_FA 
    FacilityServiceId, UserFacilityID, FacilityId 

目前,我有這個INNER JOIN

SELECT DISTINCT 
    MessageID, UserFacilityID, 9  
FROM 
    #ProviderMessages 
INNER JOIN 
    #User_FA ON (#User_FA.FacilityId = #ProviderMessages.FacilityID OR 
            #ProviderMessages.FacilityID IS NULL) 
      AND (#User_FA.FacilityServiceId = #ProviderMessages.FacilityServiceID) 
+0

使用[案例](http://msdn.microsoft.com/en-us/library/ms181765.aspx) – 2014-12-05 17:48:40

+1

我想知道這個問題有什麼不好。 -5,真的嗎? – 2016-06-26 20:45:53

回答

-1

這是您的查詢,使用表的別名,以使其更易於閱讀:

SELECT DISTINCT MessageID, UserFacilityID, 9  
FROM #ProviderMessages m INNER JOI 
    #User_FA u 
    ON (u.FacilityId = m.FacilityID OR m.FacilityID IS NULL) AND 
     u.FacilityServiceId = m.FacilityServiceID; 

你想要的邏輯是

SELECT DISTINCT MessageID, UserFacilityID, 9  
FROM #ProviderMessages m INNER JOIN 
    #User_FA u 
    ON ((pm.ServiceLevelID = 1 or pm.ServiceLevelID is null) AND pm.FacilityId = u.facilityId) or 
     (pm.ServiceLevelID = 2 and pm.FacilityServiceID = m.FacilityServiceID) 
+0

輸出錯誤?這就是你要求的。 – Paparazzi 2014-12-05 18:48:43

+0

什麼是輸出,你期待它是什麼?一個簡單的「這不是我想要的」並不能幫助任何人獲得你真正追求的解決方案。 – user2366842 2014-12-05 20:15:21

-2

聯合兩個結果


SELECT DISTINCT MessageID, UserFacilityID, 9  
FROM #ProviderMessages 
INNER JOIN #User_FA ON (#User_FA.FacilityId = #ProviderMessages.FacilityID OR 
            #ProviderMessages.FacilityID IS NULL) 
WHERE #ProviderMessages.ServiceLevel <> 2 

UNION 

SELECT DISTINCT MessageID, UserFacilityID, 9  
FROM #ProviderMessages 
INNER JOIN #User_FA ON (#User_FA.FacilityServiceId = #ProviderMessages.FacilityServiceID) 
WHERE #ProviderMessages.ServiceLevel = 2 
0
SELECT DISTINCT MessageID, UserFacilityID, 9  
FROM #ProviderMessages 
JOIN #User_FA 
    ON #ProviderMessages.ServiceLevelID in (null,1,2) 
    and ( (#ProviderMessages.ServiceLevelID in (null,1) 
      and #User_FA.FacilityId = #ProviderMessages.FacilityID) 
     or 
     (ProviderMessages.ServiceLevelID = 2 
      AND #User_FA.FacilityServiceId = #ProviderMessages.FacilityServiceID) 
    ) 
+0

在(null,1,2) 和'?中添加'#ProviderMessages,ServiceLevelID子句有什麼好處? – Beth 2014-12-05 21:19:47

+0

@貝絲我不認爲它會受傷。而且我認爲它可以讓查詢優化器儘早提出問題。 – Paparazzi 2014-12-05 21:22:52

+0

猜測它取決於規模。不過,我懷疑還有其他的價值。 – Beth 2014-12-05 21:24:53

-1

我不知道如果我的理解很好,但在我以前的工作,我們也有類似的情況。 請試試這個:

SELECT DISTINCT 
    MessageID, UserFacilityID, 9  
FROM 
    #ProviderMessages 
    INNER JOIN #User_FA 
     ON (CASE WHEN ISNULL(#ProviderMessages.ServiceLevelID, 1) = 1 
       THEN #ProviderMessages.FacilityID 
       ELSE #ProviderMessages.FacilityServiceId 
      END) 
      = (CASE WHEN ISNULL(#ProviderMessages.ServiceLevelID, 1) = 1 
       THEN #User_FA.FacilityId 
       ELSE #User_FA.FacilityServiceId 
       END) 

我希望它有幫助。