SQL Server:如何在嵌套CASE上对LIKE使用CASE

时间:2018-07-10 00:28:35

标签: sql-server

我很适应SQL,希望这里有人可以帮助我。

我有一个存储过程,我想根据STACK列是否包含某个项目名称来传递一个不同的值。 如果包含该项目,则将QTYofItems除以4,然后将该答案四舍五入到最接近的整数。如果不包含,则仅显示QTYofItems。 Please see screenshot

到目前为止,我仅在检查具有特定值的匹配项时才使用CASE,因此我不确定这一点。有人可以告诉我以下内容是否正确和正确,或者让我知道如何正确编写此内容(仅针对括号中的部分)?

SELECT        ISNULL(dbo.ItemSpecs.ConfigProptext, dbo.WorkOrderDetails.Description) AS Description, ISNULL(dbo.WorkOrderDetails.QtyOrdered, '') AS QtyofItems, CASE WHEN dbo.ItemSpecs.ConfigProptext  like '552%'  AND dbo.ItemSpecs.ConfigProptext LIKE '%leg%' AND dbo.ItemSpecs.ConfigProptext LIKE '%Sled% WHEN dbo.ItemSpecs.ConfigProptext  like '550%' and   WHEN dbo.ItemSpecs.ConfigProptext  like 'Akami%'  WHEN dbo.ItemSpecs.ConfigProptext  like 'Circus%'  WHEN dbo.ItemSpecs.ConfigProptext  like 'Coast%'  WHEN dbo.ItemSpecs.ConfigProptext  like 'Chill Bar Stool%' THEN dbo.WorkOrderDetails.QtyOrdered/4 ELSE dbo.WorkOrderDetails.QtyOrdered END 

3 个答案:

答案 0 :(得分:0)

首先,我将开始使用表别名来简化读取SQL语句的过程。

类似dbo.ItemSpecs as spec(spec将是别名,并使用它代替整个表名

case语句(假设所需逻辑正确)应该是这样的:

     CASE 
     WHEN (dbo.ItemSpecs.ConfigProptext  like '552%'  AND dbo.ItemSpecs.ConfigProptext LIKE '%leg%' AND dbo.ItemSpecs.ConfigProptext LIKE '%Sled%') THEN dbo.WorkOrderDetails.QtyOrdered/4
     WHEN dbo.ItemSpecs.ConfigProptext  like '550%' THEN dbo.WorkOrderDetails.QtyOrdered/4
     WHEN dbo.ItemSpecs.ConfigProptext  like 'Akami%'   THEN dbo.WorkOrderDetails.QtyOrdered/4
     WHEN dbo.ItemSpecs.ConfigProptext  like 'Circus%'   THEN dbo.WorkOrderDetails.QtyOrdered/4
     WHEN dbo.ItemSpecs.ConfigProptext  like 'Coast%'   THEN dbo.WorkOrderDetails.QtyOrdered/4
     WHEN dbo.ItemSpecs.ConfigProptext  like 'Chill Bar Stool%'  THEN dbo.WorkOrderDetails.QtyOrdered/4
      ELSE dbo.WorkOrderDetails.QtyOrdered 
END as stackable

答案 1 :(得分:0)

您的WHEN需要有一个THEN对应对象。

SELECT
    ISNULL(dbo.ItemSpecs.ConfigProptext, dbo.WorkOrderDetails.Description) AS Description, 
    ISNULL(dbo.WorkOrderDetails.QtyOrdered, '') AS QtyofItems, 
    CASE WHEN dbo.ItemSpecs.ConfigProptext  LIKE '552%'  
            AND dbo.ItemSpecs.ConfigProptext LIKE '%leg%' 
            AND dbo.ItemSpecs.ConfigProptext LIKE '%Sled%'
            THEN dbo.WorkOrderDetails.QtyOrdered/4  --<----------- Made assumptions here
        WHEN dbo.ItemSpecs.ConfigProptext LIKE '550%' 
            AND dbo.ItemSpecs.ConfigProptext LIKE 'Akami%'
            AND dbo.ItemSpecs.ConfigProptext LIKE 'Circus%'
            THEN dbo.WorkOrderDetails.QtyOrdered/4  --<----------- Made assumptions here
        WHEN dbo.ItemSpecs.ConfigProptext LIKE 'Coast%'
            THEN dbo.WorkOrderDetails.QtyOrdered/4  --<----------- Made assumptions here
        WHEN dbo.ItemSpecs.ConfigProptext LIKE 'Chill Bar Stool%'
            THEN dbo.WorkOrderDetails.QtyOrdered/4 
        ELSE dbo.WorkOrderDetails.QtyOrdered 
    END

您可以探索的另一种选择...

SELECT
    ...
    CASE WHEN LEFT(dbo.ItemSpecs.ConfigProptext,3) IN ('552','550')
            THEN <SomeValue>
            ...
        ELSE dbo.WorkOrderDetails.QtyOrdered 
    END

答案 2 :(得分:0)

我所做的是手动编码,例如:

CASE WHEN dbo.ItemSpecs.ConfigProptext LIKE 'Project Cantilever%' THEN dbo.WorkOrderDetails.QtyOrdered / 4 WHEN dbo.ItemSpecs.ConfigProptext
                      LIKE 'Revolution%' THEN dbo.WorkOrderDetails.QtyOrdered / 4 WHEN dbo.ItemSpecs.ConfigProptext LIKE 'Timber Cafe%' THEN dbo.WorkOrderDetails.QtyOrdered / 4 WHEN dbo.ItemSpecs.ConfigProptext LIKE 'Web%' THEN dbo.WorkOrderDetails.QtyOrdered
                      / 4 ELSE dbo.WorkOrderDetails.QtyOrdered / 4 END AS [Qty of Units]