2014-10-16 65 views
0

我想知道如何根據我的第一個選定語句返回特定結果。基本上我有兩個ID。 CustBillToID和CustShipToID。如果CustShipToID不爲空,我想選擇加入它的記錄和所有記錄。如果它爲null,則默認爲CustBillToID以及所有與之結合的結果。根據案例選擇列SQL

這是我的SQL,顯然不工作。我應該提到我試圖在條件中做一個子查詢,但因爲它返回多個結果,它將無法工作。我正在使用SQL Server 2012.

SELECT CASE WHEN cp.CustShipToID IS NOT NULL 
        THEN 
        cy.CustDesc, 
        cy.Address1, 
        cy.Address2, 
        cy.City, 
        cy.State, 
        cy.ZIP, 
        cy.Phone 
        ELSE 
        c.CustDesc, 
        c.Address1, 
        c.Address2, 
        c.City, 
        c.State, 
        c.ZIP, 
        c.Phone 
        END 
        LoadID, 
        cp.CustPOID, 
        cp.POBillToRef, 
        cp.POShipToRef, 
        cp.CustBillToID, 
        cp.CustShipToID, 
        cp.ArrivalDate, 
        cp.LoadDate, 
        cp.StopNum, 
        cp.ConfNum, 
        cp.EVNum, 
        cp.ApptNum, 
        ld.CarrId, 
        ld.Temperature, 
        cr.CarrDesc 

FROM [Sales].[dbo].[CustPO] AS cp 
       LEFT OUTER JOIN Load AS ld 
       ON cp.LoadID = ld.LoadID 
       LEFT OUTER JOIN Carrier AS cr 
       ON ld.CarrId = cr.CarrId 
       LEFT OUTER JOIN Customer AS c 
       ON c.CustId = cp.CustBillToID 
       WHERE CustPOID=5213 

任何想法?

另外我目前的SQL是在下面,我做一個條件來確定它是否設置。如果可能的話,我寧願在SQL中執行它。

SELECT cp.LoadID, 
        cp.CustPOID, 
        cp.POBillToRef, 
        cp.POShipToRef, 
        cp.CustBillToID, 
        cp.CustShipToID, 
        cp.ArrivalDate, 
        cp.LoadDate, 
        cp.StopNum, 
        cp.ConfNum, 
        cp.EVNum, 
        cp.ApptNum, 
        ld.CarrId, 
        ld.Temperature, 
        cr.CarrDesc, 
        c.CustDesc as CustBillToDesc, 
        c.Address1 as CustBillAddress1, 
        c.Address2 as CustBillAddress2, 
        c.City as CustBillCity, 
        c.State as CustBillState, 
        c.ZIP as CustBillZIP, 
        c.Phone as CustBillPhone, 
        cy.CustDesc as CustShipToDesc, 
        cy.Address1 as CustShipAddress1, 
        cy.Address2 as CustShipAddress2, 
        cy.City as CustShipCity, 
        cy.State as CustShipState, 
        cy.ZIP as CustShipZIP, 
        cy.Phone as CustShipPhone 

       FROM [Sales].[dbo].[CustPO] as cp 
       left outer join Load as ld 
       on cp.LoadID = ld.LoadID 
       left outer join Carrier as cr 
       on ld.CarrId = cr.CarrId 
       left outer join Customer as c 
       on c.CustId = cp.CustBillToID 
       left outer join Customer as cy 
       on cy.CustId = cp.CustShipToID 
       WHERE CustPOID=? 

回答

1

對於這一點,你基本上要建立一個字符串,它是你的SQL,然後執行字符串...看看@回答這一::

SQL conditional SELECT

+0

這看起來很完美,ty – Waragi 2014-10-16 20:59:00

4

需要有單獨的case爲每列:

SELECT (CASE WHEN cp.CustShipToID IS NOT NULL THEN cy.CustDesc ELSE c.CustDesc END) as CustDesc, 
     (CASE WHEN cp.CustShipToID IS NOT NULL THEN cy.Address1 ELSE c.Address1 END) as Address1, 
     (CASE WHEN cp.CustShipToID IS NOT NULL THEN cy.Address2 ELSE c.Address2 END) as Address2, 
     (CASE WHEN cp.CustShipToID IS NOT NULL THEN cy.City ELSE c.City END) as City, 
     (CASE WHEN cp.CustShipToID IS NOT NULL THEN cy.State ELSE c.State END) as State, 
     (CASE WHEN cp.CustShipToID IS NOT NULL THEN cy.ZIP ELSE c.ZIP END) as ZIP, 
     (CASE WHEN cp.CustShipToID IS NOT NULL THEN cy.Phone ELSE c.Phone END) as Phone, 
     . . . 
+0

啊,有道理。謝謝 – Waragi 2014-10-16 20:56:24

0

你嘗試合併(CustShipToID,CustBillToID)?

... 
FROM [Sales].[dbo].[CustPO] as cp 
      left outer join Load as ld 
      on cp.LoadID = ld.LoadID 
      left outer join Carrier as cr 
      on ld.CarrId = cr.CarrId 
      inner join Customer as c 
      on c.CustId = coalesce(cp.CustShipToID,cp.CustBillToID) 
...