SQL:Case in Where子句中的select use value

时间:2016-06-17 15:45:47

标签: sql sql-server select

我有一个SQL命令,在SELECT中有一个案例,它使用表中的值或只使用' 0'。

我想用那个' cased'稍后在where子句中的值,但它不能按我的意愿工作。

这就是我的意思:

System.Diagnostigs.Debug

5 个答案:

答案 0 :(得分:1)

别名仅在查询执行结束时应用 - 您不能在where子句中使用它。但是,您可以使用相同的表达式:

WHERE QtyType = 0 AND
      CASE vi.EncodeData
         WHEN 'Y' THEN ii.Quantity1
         ELSE 0
      END > 0

或者,或者,只需打开它并创建与基础列相关的条件:

WHERE QtyType = 0 AND
      vi.EncodeData = 'Y' AND 
      ii.Quantity1 > 0

答案 1 :(得分:0)

SQL(通常)不允许在后续子句中使用列别名,例如WHERE,也不允许在SELECT本身中使用列别名。通用解决方案是子查询或CTE。

另一种方法使用outer apply

SELECT DISTINCT hi.SKUNo [HostSKU] , SUBSTRING(vi.GTIN, 3, 14) [GTIN] ,
       c.[VendorNo] ,
       'Inv_Full_Sync' as [Reason Code] ,
       'Vendor Inventory Full Sync' as [Reason Text] ,
       c.[Quantity] , --< quantity
      'ONHAND' as  [OnHand] ,
      RTRIM(v.Category) as [Vendor Category]
FROM ItemInventory ii INNER JOIN
     HostItems hi
     ON hi.ItemId = ii.ItemId INNER JOIN
     VendorItems vi
     ON vi.ItemId = ii.ItemId AND vi.VendorNo = ii.VendorNo INNER JOIN
     Vendors v
     ON v.VendorNo = ii.VendorNo LEFTJOIN
     dbo.VendorGroups vg
     ON vg.VendorNo = v.VendorNo OUTER APPLY
     (SELECT (CASE vg.VendorGroup WHEN vg.VendorGroup THEN vg.VendorGroup
                     ELSE v.VendorNo
              END) as [VendorNo] ,
             (CASE vi.EncodeData WHEN 'Y' THEN ii.Quantity1 ELSE 0
              END [Quantity]
             ) 
     ) c
WHERE QtyType = 0 AND
      c.[Quantity] > 0 --< here is where it bombs..
ORDER BY VendorNo, hi.SKUNo; 

答案 2 :(得分:0)

您无法直接在WHERE上使用列别名。因此,您可以将其替换为整个CASE表达式,也可以使用派生表或CTE。

派生表:

SELECT *
FROM (  SELECT DISTINCT
            hi.SKUNo [HostSKU] ,
            SUBSTRING(vi.GTIN, 3, 14) [GTIN] ,
            CASE vg.VendorGroup
              WHEN vg.VendorGroup THEN vg.VendorGroup
              ELSE v.VendorNo
            END [VendorNo] ,
            'Inv_Full_Sync' [Reason Code] ,
            'Vendor Inventory Full Sync' [Reason Text] ,
            CASE vi.EncodeData
              WHEN 'Y' THEN ii.Quantity1
              ELSE 0
            END [Quantity] , --< quantity
            'ONHAND' [OnHand] ,
            RTRIM(v.Category) [Vendor Category]
        FROM    ItemInventory ii
            INNER JOIN HostItems hi ON hi.ItemId = ii.ItemId
            INNER JOIN VendorItems vi ON vi.ItemId = ii.ItemId
                                         AND vi.VendorNo = ii.VendorNo
            INNER JOIN Vendors v ON v.VendorNo = ii.VendorNo
            LEFT JOIN dbo.VendorGroups vg ON vg.VendorNo = v.VendorNo
            WHERE QtyType = 0) X
WHERE [Quantity] > 0 --< here is where it bombs..
ORDER BY VendorNo ,
         HostSKU; 

CTE:

WITH CTE AS
(
    SELECT DISTINCT
        hi.SKUNo [HostSKU] ,
        SUBSTRING(vi.GTIN, 3, 14) [GTIN] ,
        CASE vg.VendorGroup
          WHEN vg.VendorGroup THEN vg.VendorGroup
          ELSE v.VendorNo
        END [VendorNo] ,
        'Inv_Full_Sync' [Reason Code] ,
        'Vendor Inventory Full Sync' [Reason Text] ,
        CASE vi.EncodeData
          WHEN 'Y' THEN ii.Quantity1
          ELSE 0
        END [Quantity] , --< quantity
        'ONHAND' [OnHand] ,
        RTRIM(v.Category) [Vendor Category]
    FROM    ItemInventory ii
        INNER JOIN HostItems hi ON hi.ItemId = ii.ItemId
        INNER JOIN VendorItems vi ON vi.ItemId = ii.ItemId
                                     AND vi.VendorNo = ii.VendorNo
        INNER JOIN Vendors v ON v.VendorNo = ii.VendorNo
        LEFT JOIN dbo.VendorGroups vg ON vg.VendorNo = v.VendorNo
            WHERE QtyType = 0
)
SELECT *
FROM CTE
WHERE [Quantity] > 0 --< here is where it bombs..
ORDER BY VendorNo ,
         [HostSKU]; 

答案 3 :(得分:0)

SELECT DISTINCT
    hi.SKUNo [HostSKU] ,
    SUBSTRING(vi.GTIN, 3, 14) [GTIN] ,
    CASE vg.VendorGroup
       WHEN vg.VendorGroup THEN vg.VendorGroup
       ELSE v.VendorNo
   END [VendorNo] ,
   'Inv_Full_Sync' [Reason Code] ,
   'Vendor Inventory Full Sync' [Reason Text] ,
  CASE vi.EncodeData
     WHEN 'Y' THEN ii.Quantity1
     ELSE 0
  END [Quantity] , --< quantity
  'ONHAND' [OnHand] ,
  RTRIM(v.Category) [Vendor Category]
FROM    ItemInventory ii
INNER JOIN HostItems hi ON hi.ItemId = ii.ItemId
INNER JOIN VendorItems vi ON vi.ItemId = ii.ItemId
    AND vi.VendorNo = ii.VendorNo
INNER JOIN Vendors v ON v.VendorNo = ii.VendorNo
LEFT JOIN dbo.VendorGroups vg ON vg.VendorNo = v.VendorNo
WHERE   QtyType = 0
   CASE vi.EncodeData
      WHEN 'Y' THEN ii.Quantity1
    ELSE 0
   END > 0
ORDER BY VendorNo ,
   hi.SKUNo; 

答案 4 :(得分:0)

我假设您的数量字段为NULL,如果vi.EncodeData&lt;&gt; &#39; Y&#39;

在WHERE子句中,您可以使用ISNULL或COALESCE来短路第二个子句。

ISNULL(ii.Quantity1, 0) > 0 

-OR -

COALESCE(ii.Quantity1, 0) > 0 

在没有数量的情况下等于0> 0并将排除该行。我可能会坚持使用ISNULL,因为您只评估一列。