有人可以帮助我解密此查询吗?令人困惑的案例陈述

时间:2019-04-12 14:11:25

标签: sql sql-server case

我很难阅读此查询。我需要摆脱与“ ShowStandadrdCost”有关的所有事情,所以最外面的情况是这样。但我似乎无法弄清楚要删除的确切行。

(CASE WHEN ISNULL(ShowStandardCost, 0) = 0 THEN
                CASE WHEN ISNULL(s.Actual_Cost, 0) = 0
                        THEN ISNULL(s.Standard_Cost, 0)
                        ELSE s.Actual_Cost  
                END
             ELSE ISNULL(s.Standard_Cost, 0)
        END + (CASE WHEN ISNULL(ShowStandardCost, 0) = 0 THEN
                CASE WHEN ISNULL(s.Actual_Cost, 0) = 0
                        THEN ISNULL(s.Standard_Cost, 0)
                        ELSE s.Actual_Cost  
                END
             ELSE ISNULL(s.Standard_Cost, 0)
        END ) Item_Cost_Per,

整个查询只是

CASE WHEN ISNULL(s.Actual_Cost, 0) = 0
                            THEN ISNULL(s.Standard_Cost, 0)
                            ELSE s.Actual_Cost

我不知道要添加的内容。

3 个答案:

答案 0 :(得分:0)

这可能就是我写表达式的方式。

CASE WHEN ShowStandardCost <> 0 --Maybe (ShowStandardCost = 1)?
    THEN ISNULL(s.Standard_Cost, 0)
    ELSE --Show Actual Cost, unless it's zero or null. 
         --If so, show Standard_Cost unless it's null. 
         --Only show zero if both columns are zero or null.
        COALESCE( NULLIF( s.Actual_Cost, 0), s.Standard_Cost, 0)
    END * 2 --The CASE expression is just repeating itself.

答案 1 :(得分:0)

尝试一下,不确定是否要这样,

CASE WHEN ISNULL(s.Actual_Cost, 0) = 0 THEN ISNULL(s.Standard_Cost, 0) ELSE s.Actual_Cost end Item_Cost_Per

以下实际评论,

/*(CASE WHEN ISNULL(ShowStandardCost, 0) = 0 THEN */
                CASE WHEN ISNULL(s.Actual_Cost, 0) = 0
                        THEN ISNULL(s.Standard_Cost, 0)
                        ELSE s.Actual_Cost  
                END
            /* ELSE ISNULL(s.Standard_Cost, 0)
        END + (CASE WHEN ISNULL(ShowStandardCost, 0) = 0 THEN
                CASE WHEN ISNULL(s.Actual_Cost, 0) = 0
                        THEN ISNULL(s.Standard_Cost, 0)
                        ELSE s.Actual_Cost  
                END
             ELSE ISNULL(s.Standard_Cost, 0)
        END ) */ Item_Cost_Per,

答案 2 :(得分:0)

If ShowStandardCost = null Then
   If Actual_Cost =  null Then
       If Standard_Cost = null Then 0 Else Standard_Cost
   Else Actual_Cost
End

然后,该语句重复并将该值添加到自身。我只是将第一部分用括号括起来,如果是我的话,最后加上“ * 2”。

(CASE WHEN ISNULL(ShowStandardCost, 0) = 0 THEN
         CASE WHEN ISNULL(s.Actual_Cost, 0) = 0
                THEN ISNULL(s.Standard_Cost, 0)
                ELSE s.Actual_Cost  
         END
      ELSE ISNULL(s.Standard_Cost, 0)
  END) * 2