根据案例SQL选择列

时间:2014-10-16 20:50:46

标签: sql sql-server sql-server-2012 conditional

我想知道如何根据我的第一个选定的陈述返回特定的结果。基本上我有两个ID。 CustBillToID和CustShipToID。如果CustShipToID不为null,我想选择它和连接到它的所有记录。如果它是null,则默认为CustBillToID以及与之相连的所有结果。

这是我的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=?

3 个答案:

答案 0 :(得分: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,
       . . . 

答案 1 :(得分:1)

为此,你基本上想要构建一个作为你的SQL的字符串然后执行字符串...看看@这个答案::

SQL conditional SELECT

答案 2 :(得分: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 )
...