带绝对值的CASE语句

时间:2017-05-17 14:36:38

标签: sql tsql

CASE语句似乎没有捕获具有负值的记录,即使我在表达式上使用ABS()函数。有问题的记录是值为-1.52的记录,它应落在案例陈述“

的范围内
WHEN ABS(DifferenceHours - @Mean_Diff) >1*@SD
                     AND ABS(DifferenceHours - @Mean_Diff) <=2*@SD
                     THEN 'Recommend Review'

有人可以对此有所了解吗?提前致谢! (抱歉预期结果的格式化)

创建包含样本数据的表的脚本:

CREATE TABLE [dbo].[Sample_Data](
    [ID] [char](6) NOT NULL,
    [Actual_Hours] [decimal](38, 2) NULL,
    [Standard_Hours] [decimal](10, 2) NULL,
    [DifferenceHours] [decimal](38, 2) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
INSERT [dbo].[Sample_Data] ([ID], [Actual_Hours], [Standard_Hours], [DifferenceHours]) VALUES (N'490412', CAST(1.22 AS Decimal(38, 2)), CAST(0.92 AS Decimal(10, 2)), CAST(0.30 AS Decimal(38, 2)))
INSERT [dbo].[Sample_Data] ([ID], [Actual_Hours], [Standard_Hours], [DifferenceHours]) VALUES (N'491712', CAST(1.36 AS Decimal(38, 2)), CAST(0.92 AS Decimal(10, 2)), CAST(0.44 AS Decimal(38, 2)))
INSERT [dbo].[Sample_Data] ([ID], [Actual_Hours], [Standard_Hours], [DifferenceHours]) VALUES (N'493822', CAST(1.96 AS Decimal(38, 2)), CAST(0.92 AS Decimal(10, 2)), CAST(1.04 AS Decimal(38, 2)))
INSERT [dbo].[Sample_Data] ([ID], [Actual_Hours], [Standard_Hours], [DifferenceHours]) VALUES (N'496762', CAST(1.51 AS Decimal(38, 2)), CAST(1.84 AS Decimal(10, 2)), CAST(-0.33 AS Decimal(38, 2)))
INSERT [dbo].[Sample_Data] ([ID], [Actual_Hours], [Standard_Hours], [DifferenceHours]) VALUES (N'497082', CAST(2.72 AS Decimal(38, 2)), CAST(0.92 AS Decimal(10, 2)), CAST(1.80 AS Decimal(38, 2)))
INSERT [dbo].[Sample_Data] ([ID], [Actual_Hours], [Standard_Hours], [DifferenceHours]) VALUES (N'497092', CAST(1.45 AS Decimal(38, 2)), CAST(0.92 AS Decimal(10, 2)), CAST(0.53 AS Decimal(38, 2)))
INSERT [dbo].[Sample_Data] ([ID], [Actual_Hours], [Standard_Hours], [DifferenceHours]) VALUES (N'497162', CAST(2.06 AS Decimal(38, 2)), CAST(0.92 AS Decimal(10, 2)), CAST(1.14 AS Decimal(38, 2)))
INSERT [dbo].[Sample_Data] ([ID], [Actual_Hours], [Standard_Hours], [DifferenceHours]) VALUES (N'498002', CAST(2.03 AS Decimal(38, 2)), CAST(0.92 AS Decimal(10, 2)), CAST(1.11 AS Decimal(38, 2)))
INSERT [dbo].[Sample_Data] ([ID], [Actual_Hours], [Standard_Hours], [DifferenceHours]) VALUES (N'498632', CAST(0.89 AS Decimal(38, 2)), CAST(0.92 AS Decimal(10, 2)), CAST(-0.03 AS Decimal(38, 2)))
INSERT [dbo].[Sample_Data] ([ID], [Actual_Hours], [Standard_Hours], [DifferenceHours]) VALUES (N'499162', CAST(0.79 AS Decimal(38, 2)), CAST(1.84 AS Decimal(10, 2)), CAST(-1.05 AS Decimal(38, 2)))
INSERT [dbo].[Sample_Data] ([ID], [Actual_Hours], [Standard_Hours], [DifferenceHours]) VALUES (N'499582', CAST(1.19 AS Decimal(38, 2)), CAST(0.92 AS Decimal(10, 2)), CAST(0.27 AS Decimal(38, 2)))

有问题的代码:

我正在努力克服堆栈交换的格式。当我将代码粘贴到其中时,插入换行符并删除“一些”星号。我不知道为什么。我在这里发布了代码,而我试图将其整理出来。有趣的是,创建包含示例数据的表的代码似乎没有问题。

https://codedump.io/share/7ISZ7p8qrXvu/1

FROM Sample_Data

我的预期结果:

ID     | Actual_Hours | Standard_Hours | DifferenceHours | Deviation | Recommendation
490412 | 1.22        | 0.92            | 0.30           | -0.17 |
491712 | 1.36        | 0.92            | 0.44           | -0.03 |
493822 | 1.96        | 0.92            | 1.04           | 0.57  | 
496762 | 1.51        | 1.84            |-0.33 | -0.80   |
497082 | 2.72        | 0.92            | 1.80 | 1.33    |   Recommend Review
497092 | 1.45        | 0.92            | 0.53 | 0.06    |
497162 | 2.06        | 0.92            | 1.14 | 0.67    | 
498002 | 2.03        | 0.92            | 1.11 | 0.64    |
498632 | 0.89        | 0.92            |-0.03 | -0.50   | 
499162 | 0.79        | 1.84            | -1.05| -1.52   | RecommendReview <- 
499582 | 1.19        | 0.92            | 0.27 | -0.20   | 

2 个答案:

答案 0 :(得分:0)

使用这个特定的行值,其中Actual_Hours = .79和Standard_Hours = 1.84我们评估数学,我们得到......

WHEN ((Actual_Hours - Standard_Hours)/Standard_Hours)* 100>25 THEN
    CASE ...
    ELSE ''
ELSE ''

将数据输入计算:

((.79 - 1.84)/1.84) * 100

简化:

(-.57) * 100
-57

由于此值为负,因此不评估为&gt; 25。因此,您的第一个案例不正确,并评估为ELSE(您的空白字符串&#39;&#39;)。你会在第4行和第9行也注意到同样的问题。

要解决,您可能(可能?)也希望将您的第一个CASE包装在ABS中。但这可能取决于您的业务逻辑。

为了全面披露我的测试方式,我在搜索中添加了这两列:

DifferenceHours - @Mean_Diff AS CaseTestResult,
((Actual_Hours - Standard_Hours)/Standard_Hours)* 100 AS HoursCalc

这使我能够将CASE语句与结果进行比较,然后再回到解决方案中。

答案 1 :(得分:0)

我认为问题不在于你的案例陈述而是标准偏差。

SD为0.75然后2 * 0.75 = 1.50而不是1.52

试一试:

SELECT *        
    , DifferenceHours - @Mean_Diff AS 'Deviation'    
    , CASE                                                
        WHEN ((Actual_Hours - Standard_Hours)/Standard_Hours)*100>25
            THEN          
                CASE 
                    WHEN ABS(DifferenceHours - @Mean_Diff)<=1*@SD
                    THEN ''

                    WHEN ABS(DifferenceHours - @Mean_Diff) >1*@SD
                    AND ABS(DifferenceHours - @Mean_Diff) <=2*@SD
                    THEN 'Recommend Review'

                    WHEN ABS(DifferenceHours - @Mean_Diff) > 2*@SD
                    THEN 'Strongly Recommend Review'
                ELSE ''
                END
        ELSE ''
        END AS 'Recommendation'
        ,ABS(DifferenceHours - @Mean_Diff) [ABS(DifferenceHours - @Mean_Diff) ]
        , 1*@SD [1*@SD] 
        , 2.*@SD [2*@SD]

FROM Sample_Data