簡體   English   中英

SQL:如何在聯接中使用CASE

[英]SQL: How to use CASE in join

這是我正在嘗試使用的代碼

SELECT
  TData.dtContractDate,
  TData.chPurchase,
  TData.nShipperID,
  TData.nSourceSiteId,
  Vendor.chShipperName,
  Vendor.chCity,
  Vendor.chState,
  Vendor.chCountry,
  Vendor.chZip 
FROM Data1.dbo.TData TData
JOIN Data1.dbo.Vendor
  ON Vendor.nShipperId = TData.nShipperID
  AND Vendor.nCompanyCode  = (CASE WHEN Vendor.nCompanyCode = '2' THEN '2' ELSE '26' END)
WHERE (TData.nCompanyCode=2) AND (TData.dtContractDate > '" 04/23/2015 12:01:00 AM')

我正在嘗試加入這兩個表,但是如果

ncompanycode = 2 AND Vendor.nShipperId = TData.nShipperID

那我想拉那些數據,如果這個不存在,那我想用

ncompanycode = 26 and Vendor.nShipperId = TData.nShipperID

現在,我得到了兩家公司擁有的所有shipperID副本。

這是虛擬表以及我得到的結果,我不希望chPurchase重復。

TData表

|            dtContractDate | chPurchase | nShipperID | nSourceSiteId | nCompanyCode |
|---------------------------|------------|------------|---------------|--------------|
| January, 04 2015 00:00:00 |    2547635 |        453 |      68686868 |            2 |
| January, 05 2015 00:00:00 |    2547636 |        453 |      68686868 |            2 |
| January, 06 2015 00:00:00 |    2547637 |        454 |      68686868 |            2 |
| January, 07 2015 00:00:00 |    2547638 |        454 |      68686868 |            2 |
| January, 08 2015 00:00:00 |    2547639 |        455 |      68686868 |            2 |
| January, 09 2015 00:00:00 |    2547640 |        456 |      68686868 |            2 |

供應商表

| nCompanyCode | nShipperID | chShipperName | chCity | chState | chCountry |  chZip |
|--------------|------------|---------------|--------|---------|-----------|--------|
|            2 |        453 |         Name1 | Dallas |      TX |        US | 666555 |
|           26 |        453 |         Name2 | Dallas |      TX |        US | 666556 |
|            2 |        454 |         Name3 | Dallas |      TX |        US | 666557 |
|           26 |        455 |         Name4 | Dallas |      TX |        US | 666558 |
|            2 |        455 |         Name5 | Dallas |      TX |        US | 666559 |
|           26 |        456 |         Name6 | Dallas |      TX |        US | 666560 |

結果表

|   dtContractDate | chPurchase | nShipperID | nSourceSiteId | nCompanyCode | chShipperName | chCity | chState | chCountry |  chZip |
|------------------|------------|------------|---------------|--------------|---------------|--------|---------|-----------|--------|
| January, 04 2015 |    2547635 |        453 |      68686868 |           26 |         Name2 | Dallas |      TX |        US | 666555 |
| January, 04 2015 |    2547635 |        453 |      68686868 |            2 |         Name1 | Dallas |      TX |        US | 666555 |
| January, 05 2015 |    2547636 |        453 |      68686868 |           26 |         Name1 | Dallas |      TX |        US | 666556 |
| January, 05 2015 |    2547636 |        453 |      68686868 |            2 |         Name2 | Dallas |      TX |        US | 666556 |
| January, 06 2015 |    2547637 |        454 |      68686868 |            2 |         Name3 | Dallas |      TX |        US | 666557 |
| January, 07 2015 |    2547638 |        454 |      68686868 |            2 |         Name3 | Dallas |      TX |        US | 666558 |
| January, 08 2015 |    2547639 |        455 |      68686868 |            2 |         Name5 | Dallas |      TX |        US | 666559 |
| January, 08 2015 |    2547639 |        455 |      68686868 |           26 |         Name5 | Dallas |      TX |        US | 666559 |
| January, 09 2015 |    2547640 |        456 |      68686868 |            26 |         Name6 | Dallas |      TX |        US | 666560 |

此解決方案不使用CASE但使用COALESCE在更干凈的條件下產生期望的結果。

為了縮短可讀性,我已經縮短了表別名。

我還注釋了涉及日期的WHERE子句,因為:

  1. 格式不正確,不會與當前錯別字匹配任何日期。
  2. 糾正錯別字后,它將消除樣本數據中的所有行。

SQL代碼使用兩個LEFT JOINs並使用COALESCE選擇在SELECT輸出中使用哪個。 當第一個JOINV )結果為NULL ,輸出求助於V2 V2 JOIN明確要求nCompanyCode26 ,但是如果存在多個替代nCompanyCodes則可以根據需要添加或刪除任何條件。

查詢:

SELECT
   T.dtContractDate
  ,T.chPurchase
  ,T.nShipperID
  ,T.nSourceSiteId
  ,COALESCE(V.chShipperName,V2.chShipperName) chShipperName
  ,COALESCE(V.chCity,V2.chCity) chCity
  ,COALESCE(V.chState, V2.chState) chState
  ,COALESCE(V.chCountry,V2.chCountry) chCountry
  ,COALESCE(V.chZip,V2.chZip) chZip
  -- Added to show what Vendor company code is being used
  ,COALESCE(V.nCompanyCode,V2.nCompanyCode) nCompanyCode
FROM TData T
LEFT JOIN Vendor V
  ON V.nShipperId = T.nShipperID
  AND V.nCompanyCode = T.nCompanyCode
LEFT JOIN Vendor V2
  ON V2.nShipperId = T.nShipperID
  AND V2.nCompanyCode <> T.nCompanyCode
  AND V2.nCompanyCode = 26
-- Commented out this WHERE clause because it eliminates all rows
-- WHERE T.dtContractDate > '04/23/2015 12:01:00 AM'

樣本輸出:

|   dtContractDate | chPurchase | nShipperID | nSourceSiteId | chShipperName | chCity | chState | chCountry |  chZip | nCompanyCode |
|------------------|------------|------------|---------------|---------------|--------|---------|-----------|--------|--------------|
| January, 04 2015 |    2547635 |        453 |      68686868 |         Name1 | Dallas |      TX |        US | 666555 |            2 |
| January, 05 2015 |    2547636 |        453 |      68686868 |         Name1 | Dallas |      TX |        US | 666555 |            2 |
| January, 06 2015 |    2547637 |        454 |      68686868 |         Name3 | Dallas |      TX |        US | 666557 |            2 |
| January, 07 2015 |    2547638 |        454 |      68686868 |         Name3 | Dallas |      TX |        US | 666557 |            2 |
| January, 08 2015 |    2547639 |        455 |      68686868 |         Name5 | Dallas |      TX |        US | 666559 |            2 |
| January, 09 2015 |    2547640 |        456 |      68686868 |         Name6 | Dallas |      TX |        US | 666560 |           26 |

你可以用SQLFiddle在這個例子中發揮這里

我認為您想要的是將兩個托運人ID相等的表連接起來,然后從該集合中選擇公司代碼等於您想要的值的所有行。 另外,似乎您不希望WHERE (TData.nCompanyCode=2)條件,因為您已經在嘗試選擇公司代碼在2或26以上的位置。 嘗試這個:

SELECT TData.dtContractDate, TData.chPurchase, TData.nShipperID, 
TData.nSourceSiteId,  Vendor.chShipperName, Vendor.chCity,     
Vendor.chState,  Vendor.chCountry, Vendor.chZip 
FROM Data1.dbo.TData TData
Join Data1.dbo.Vendor on Vendor.nShipperId = TData.nShipperID
WHERE Vendor.nCompanyCode  = (case when Vendor.nCompanyCode = '2' then     
'2' else '26' end)
AND (TData.dtContractDate > '" 04/23/2015     
12:01:00 AM')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM