2014-10-16 71 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) 
...