0

我有以下 SQL 语句,它不能确定字段的值何时应为“Y”。这是SQL...

SELECT A.BUSINESS_UNIT 
 , A.WO_ID 
 , A.WO_TASK_ID 
 , A.ENS_TSK_CRT_VAL 
 , (CASE WHEN A.ENS_TSK_CRT_V01 = 'Y' THEN B.DESCR ELSE ' ' END) AS ENS_TSK_CRT_V01  
 , (CASE WHEN A.ENS_TSK_CRT_V02 = 'Y' THEN C.DESCR ELSE ' ' END) AS ENS_TSK_CRT_V02 
 , (CASE WHEN A.ENS_TSK_CRT_V03 = 'Y' THEN D.DESCR ELSE ' ' END) AS ENS_TSK_CRT_V03  
 , (CASE WHEN A.ENS_TSK_CRT_V04 = 'Y' THEN E.DESCR ELSE ' ' END) AS ENS_TSK_CRT_V04 
 , (CASE WHEN A.ENS_TSK_CRT_V05 = 'Y' THEN F.DESCR ELSE ' ' END) AS ENS_TSK_CRT_V05 
 , (CASE WHEN A.ENS_TSK_CRT_V06 = 'Y' THEN G.DESCR ELSE ' ' END) AS ENS_TSK_CRT_V06 
 , (CASE WHEN A.ENS_TSK_CRT_V07 = 'Y' THEN H.DESCR ELSE ' ' END) AS ENS_TSK_CRT_V07 
 , (CASE WHEN A.ENS_TSK_CRT_V08 = 'Y' THEN I.DESCR ELSE ' ' END) AS ENS_TSK_CRT_V08 
 FROM PS_ENS_WM_TSKCR_VW A LEFT OUTER JOIN ( 
   SELECT A.BUSINESS_UNIT 
   , A.WO_TASK_ID 
   , C.DESCR 
   , A.WO_ID 
    , C.ENS_TSK_CRT_CD 
     FROM PS_ENS_WM_TSKCR_VW A 
     , PS_ENS_WM_TSK_CRT C 
    WHERE A.ENS_TSK_CRT_V01 = 'Y' 
      AND C.SETID = 'SHARE' 
      AND C.ENS_TSK_CRT_CD = '01' ) B ON B.BUSINESS_UNIT = A.BUSINESS_UNIT 
      AND B.WO_ID = A.WO_ID 
      AND B.WO_TASK_ID = A.WO_TASK_ID LEFT OUTER JOIN ( 
    SELECT A.BUSINESS_UNIT 
    , A.WO_TASK_ID 
    , C.DESCR 
    , A.WO_ID 
    , C.ENS_TSK_CRT_CD 
     FROM PS_ENS_WM_TSKCR_VW A 
     , PS_ENS_WM_TSK_CRT C 
    WHERE A.ENS_TSK_CRT_V01 = 'Y' 
      AND C.SETID = 'SHARE' 
      AND C.ENS_TSK_CRT_CD = '02' ) C ON C.BUSINESS_UNIT = A.BUSINESS_UNIT 
      AND C.WO_ID = A.WO_ID 
      AND C.WO_TASK_ID = A.WO_TASK_ID LEFT OUTER JOIN ( 
    SELECT A.BUSINESS_UNIT 
    , A.WO_TASK_ID 
    , C.DESCR 
    , A.WO_ID 
    , C.ENS_TSK_CRT_CD 
     FROM PS_ENS_WM_TSKCR_VW A 
     , PS_ENS_WM_TSK_CRT C 
    WHERE A.ENS_TSK_CRT_V01 = 'Y' 
      AND C.SETID = 'SHARE' 
      AND C.ENS_TSK_CRT_CD = '03' ) D ON D.BUSINESS_UNIT = A.BUSINESS_UNIT 
      AND D.WO_ID = A.WO_ID 
      AND D.WO_TASK_ID = A.WO_TASK_ID LEFT OUTER JOIN ( 
    SELECT A.BUSINESS_UNIT 
    , A.WO_TASK_ID 
    , C.DESCR 
    , A.WO_ID 
    , C.ENS_TSK_CRT_CD 
     FROM PS_ENS_WM_TSKCR_VW A 
     , PS_ENS_WM_TSK_CRT C 
    WHERE A.ENS_TSK_CRT_V01 = 'Y' 
      AND C.SETID = 'SHARE' 
      AND C.ENS_TSK_CRT_CD = '04' ) E ON E.BUSINESS_UNIT = A.BUSINESS_UNIT 
             AND E.WO_ID = A.WO_ID 
      AND E.WO_TASK_ID = A.WO_TASK_ID LEFT OUTER JOIN ( 
    SELECT A.BUSINESS_UNIT 
    , A.WO_TASK_ID 
    , C.DESCR 
    , A.WO_ID 
    , C.ENS_TSK_CRT_CD 
     FROM PS_ENS_WM_TSKCR_VW A 
     , PS_ENS_WM_TSK_CRT C 
    WHERE A.ENS_TSK_CRT_V01 = 'Y' 
      AND C.SETID = 'SHARE' 
      AND C.ENS_TSK_CRT_CD = '05' ) F ON F.BUSINESS_UNIT = A.BUSINESS_UNIT 
      AND F.WO_ID = A.WO_ID 
      AND F.WO_TASK_ID = A.WO_TASK_ID LEFT OUTER JOIN ( 
    SELECT A.BUSINESS_UNIT 
    , A.WO_TASK_ID 
    , C.DESCR 
    , A.WO_ID 
    , C.ENS_TSK_CRT_CD 
     FROM PS_ENS_WM_TSKCR_VW A 
     , PS_ENS_WM_TSK_CRT C 
    WHERE A.ENS_TSK_CRT_V01 = 'Y' 
      AND C.SETID = 'SHARE' 
      AND C.ENS_TSK_CRT_CD = '06' ) G ON G.BUSINESS_UNIT = A.BUSINESS_UNIT 
      AND G.WO_ID = A.WO_ID 
      AND G.WO_TASK_ID = A.WO_TASK_ID LEFT OUTER JOIN ( 
    SELECT A.BUSINESS_UNIT 
    , A.WO_TASK_ID 
    , C.DESCR 
    , A.WO_ID 
    , C.ENS_TSK_CRT_CD 
     FROM PS_ENS_WM_TSKCR_VW A 
     , PS_ENS_WM_TSK_CRT C 
    WHERE A.ENS_TSK_CRT_V01 = 'Y' 
      AND C.SETID = 'SHARE' 
      AND C.ENS_TSK_CRT_CD = '07' ) H ON H.BUSINESS_UNIT = A.BUSINESS_UNIT 
      AND H.WO_ID = A.WO_ID 
      AND H.WO_TASK_ID = A.WO_TASK_ID LEFT OUTER JOIN ( 
    SELECT A.BUSINESS_UNIT 
    , A.WO_TASK_ID 
    , C.DESCR 
    , A.WO_ID 
    , C.ENS_TSK_CRT_CD 
     FROM PS_ENS_WM_TSKCR_VW A 
     , PS_ENS_WM_TSK_CRT C 
    WHERE A.ENS_TSK_CRT_V01 = 'Y' 
      AND C.SETID = 'SHARE' 
      AND C.ENS_TSK_CRT_CD = '08' ) I ON I.BUSINESS_UNIT = A.BUSINESS_UNIT 
      AND I.WO_ID = A.WO_ID 
      AND I.WO_TASK_ID = A.WO_TASK_ID  
    WHERE ( A.ENS_TSK_CRT_VAL <> 0 
    AND A.WO_ID = '0000002151'
    AND A.BUSINESS_UNIT = 'R3851' )

case 语句有时会选择“Y”并按预期运行,有时则不会。正在检查“Y”值的字段是长度为一个字符的 CHAR 字段。我尝试过强制转换和转换,以确保与“Y”相比,该值是我在 case 语句中所期望的值。我尝试将 '>' 和 '<' 连接到任一侧,以查看是否有空格填充该值,但事实并非如此。看起来好像案例陈述只是停止了工作。这是数据看起来的一个小样本,让您了解它的工作原理……抱歉,它有点混乱。

WO_TASK_ID  WO_ID   ENS_TSK_CRT_VAL ENS_TSK_CRT_V01 ENS_TSK_CRT_V02 ENS_TSK_CRT_V03
1           0000002151     2    Y   N   N
9           0000002151   12 N   Y   Y
12          0000002151     52   N   Y   N

从那以后,我通过采用完全不同的路线解决了这个问题,但我只想知道这是如何发生的,以及在将来再次使用 CASE 表达式时如何解决这个问题。

任何帮助是极大的赞赏...

  • 弗林
4

4 回答 4

1

可能这对您有帮助-

;WITH cte AS 
(
    SELECT  A.BUSINESS_UNIT ,
            A.WO_TASK_ID ,
            C.DESCR ,
            A.WO_ID ,
            C.ENS_TSK_CRT_CD
    FROM PS_ENS_WM_TSKCR_VW A ,
         PS_ENS_WM_TSK_CRT C
    WHERE A.ENS_TSK_CRT_V01 = 'Y'
        AND C.SETID = 'SHARE'
        AND C.ENS_TSK_CRT_CD IN (
                    '01', '02', '03', '04', 
                    '05', '06', '07', '08'
                )
)
SELECT  A.BUSINESS_UNIT ,
        A.WO_ID ,
        A.WO_TASK_ID ,
        A.ENS_TSK_CRT_VAL ,
        ISNULL(B.DESCR, ' ') AS ENS_TSK_CRT_V01 ,
        ISNULL(C.DESCR, ' ') AS ENS_TSK_CRT_V02 ,
        ISNULL(D.DESCR, ' ') AS ENS_TSK_CRT_V03 ,
        ISNULL(E.DESCR, ' ') AS ENS_TSK_CRT_V04 ,
        ISNULL(F.DESCR, ' ') AS ENS_TSK_CRT_V05 ,
        ISNULL(G.DESCR, ' ') AS ENS_TSK_CRT_V06 ,
        ISNULL(H.DESCR, ' ') AS ENS_TSK_CRT_V07 ,
        ISNULL(I.DESCR, ' ') AS ENS_TSK_CRT_V08
FROM PS_ENS_WM_TSKCR_VW A
LEFT JOIN cte B ON B.BUSINESS_UNIT = A.BUSINESS_UNIT
    AND B.WO_ID = A.WO_ID
    AND B.WO_TASK_ID = A.WO_TASK_ID
    AND B.ENS_TSK_CRT_CD = '01'
    AND A.ENS_TSK_CRT_V01 = 'Y'
LEFT JOIN cte C ON C.BUSINESS_UNIT = A.BUSINESS_UNIT
    AND C.WO_ID = A.WO_ID
    AND C.WO_TASK_ID = A.WO_TASK_ID
    AND C.ENS_TSK_CRT_CD = '02'
    AND A.ENS_TSK_CRT_V02 = 'Y'
LEFT JOIN cte D ON D.BUSINESS_UNIT = A.BUSINESS_UNIT
    AND D.WO_ID = A.WO_ID
    AND D.WO_TASK_ID = A.WO_TASK_ID
    AND D.ENS_TSK_CRT_CD = '03'
    AND A.ENS_TSK_CRT_V03 = 'Y'
LEFT JOIN cte E ON E.BUSINESS_UNIT = A.BUSINESS_UNIT
    AND E.WO_ID = A.WO_ID
    AND E.WO_TASK_ID = A.WO_TASK_ID
    AND E.ENS_TSK_CRT_CD = '04'
    AND A.ENS_TSK_CRT_V04 = 'Y'
LEFT JOIN cte F ON F.BUSINESS_UNIT = A.BUSINESS_UNIT
    AND F.WO_ID = A.WO_ID
    AND F.WO_TASK_ID = A.WO_TASK_ID
    AND F.ENS_TSK_CRT_CD = '05'
    AND A.ENS_TSK_CRT_V05 = 'Y'
LEFT JOIN cte G ON G.BUSINESS_UNIT = A.BUSINESS_UNIT
    AND G.WO_ID = A.WO_ID
    AND G.WO_TASK_ID = A.WO_TASK_ID
    AND G.ENS_TSK_CRT_CD = '06'
    AND A.ENS_TSK_CRT_V06 = 'Y'
LEFT JOIN cte H ON H.BUSINESS_UNIT = A.BUSINESS_UNIT
    AND H.WO_ID = A.WO_ID
    AND H.WO_TASK_ID = A.WO_TASK_ID
    AND H.ENS_TSK_CRT_CD = '07'
    AND A.ENS_TSK_CRT_V07 = 'Y'
LEFT JOIN cte I ON I.BUSINESS_UNIT = A.BUSINESS_UNIT
    AND I.WO_ID = A.WO_ID
    AND I.WO_TASK_ID = A.WO_TASK_ID
    AND I.ENS_TSK_CRT_CD = '08'
    AND A.ENS_TSK_CRT_V08 = 'Y'
WHERE A.ENS_TSK_CRT_VAL != 0
    AND A.WO_ID = '0000002151'
    AND A.BUSINESS_UNIT = 'R3851'
于 2013-05-24T06:41:48.167 回答
1

顺便说一句,以防万一有人好奇,这是我在将关于 CASE 表达式的问题发布到原始问题之前使用的解决方法......

SELECT A.BUSINESS_UNIT , A.WO_ID , A.WO_TASK_ID , A.ENS_TSK_CRT_VAL 
 , (CASE WHEN (CASE WHEN CAST(A.ENS_TSK_CRT_VAL AS INT) & POWER(2,1) > 0 THEN 1 ELSE 0 END) = 1 THEN (SELECT DESCR FROM PS_ENS_WM_TSK_CRT WHERE ENS_TSK_CRT_CD = 01 AND SETID = 'SHARE') ELSE ' ' END) AS ENS_TSK_CRT_VAL01
 , (CASE WHEN (CASE WHEN CAST(A.ENS_TSK_CRT_VAL AS INT) & POWER(2,2) > 0 THEN 1 ELSE 0 END) = 1 THEN (SELECT DESCR FROM PS_ENS_WM_TSK_CRT WHERE ENS_TSK_CRT_CD = 02 AND SETID = 'SHARE') ELSE ' ' END) AS ENS_TSK_CRT_VAL02
 , (CASE WHEN (CASE WHEN CAST(A.ENS_TSK_CRT_VAL AS INT) & POWER(2,3) > 0 THEN 1 ELSE 0 END) = 1 THEN (SELECT DESCR FROM PS_ENS_WM_TSK_CRT WHERE ENS_TSK_CRT_CD = 03 AND SETID = 'SHARE') ELSE ' ' END) AS ENS_TSK_CRT_VAL03
 , (CASE WHEN (CASE WHEN CAST(A.ENS_TSK_CRT_VAL AS INT) & POWER(2,4) > 0 THEN 1 ELSE 0 END) = 1 THEN (SELECT DESCR FROM PS_ENS_WM_TSK_CRT WHERE ENS_TSK_CRT_CD = 04 AND SETID = 'SHARE') ELSE ' ' END) AS ENS_TSK_CRT_VAL04
 , (CASE WHEN (CASE WHEN CAST(A.ENS_TSK_CRT_VAL AS INT) & POWER(2,5) > 0 THEN 1 ELSE 0 END) = 1 THEN (SELECT DESCR FROM PS_ENS_WM_TSK_CRT WHERE ENS_TSK_CRT_CD = 05 AND SETID = 'SHARE') ELSE ' ' END) AS ENS_TSK_CRT_VAL05
 , (CASE WHEN (CASE WHEN CAST(A.ENS_TSK_CRT_VAL AS INT) & POWER(2,6) > 0 THEN 1 ELSE 0 END) = 1 THEN (SELECT DESCR FROM PS_ENS_WM_TSK_CRT WHERE ENS_TSK_CRT_CD = 06 AND SETID = 'SHARE') ELSE ' ' END) AS ENS_TSK_CRT_VAL06
 , (CASE WHEN (CASE WHEN CAST(A.ENS_TSK_CRT_VAL AS INT) & POWER(2,7) > 0 THEN 1 ELSE 0 END) = 1 THEN (SELECT DESCR FROM PS_ENS_WM_TSK_CRT WHERE ENS_TSK_CRT_CD = 07 AND SETID = 'SHARE') ELSE ' ' END) AS ENS_TSK_CRT_VAL07
 , (CASE WHEN (CASE WHEN CAST(A.ENS_TSK_CRT_VAL AS INT) & POWER(2,8) > 0 THEN 1 ELSE 0 END) = 1 THEN (SELECT DESCR FROM PS_ENS_WM_TSK_CRT WHERE ENS_TSK_CRT_CD = 08 AND SETID = 'SHARE') ELSE ' ' END) AS ENS_TSK_CRT_VAL08
 FROM PS_ENS_WM_TSKCR_WO A 
WHERE A.ENS_TSK_CRT_VAL <> 0
AND A.WO_ID = '0000002151' AND A.BUSINESS_UNIT = 'R3851'
于 2013-05-24T14:29:56.630 回答
0

我怀疑这是案例陈述的行为。我刚刚发现 2008 R2 与 2005 的区别,在我的搜索中遇到了您的问题。case 由 sql server 2008 R2处理,与2005不同。请注意 2008 文档注释中的语句:“在某些情况下,表达式在 CASE 语句接收表达式结果作为其输入之前进行评估。评估这些表达式时可能会出错。”

因此,在 2008 年,即使不合适(输入未评估为真),也可能会评估所有潜在输出,这在升级到 2008 年之后现在在我的一些存储过程中发生。

因此,也许在您的情况下,无论 B.DESCR、C.DESCR、D.DESCR 等是否等于 'Y'、B.DESCR、C.DESCR、D.DESCR 等都得到评估,并且当时可能是null 或以其他方式无法与“Y”进行比较,从而导致该部分语句失败(这似乎是沉默的)。不确定,但它闻起来和我遇到的问题一样。唯一让我对这个“答案”持谨慎态度的是我的问题在 where 子句中,而你的问题在 sql 语句的 select 部分。

无论如何,也许有人会觉得这很有帮助。花了一些时间才弄清楚这一点。

于 2013-05-29T18:54:31.060 回答
-1

您可以尝试从 Y 中删除尾随空格,这可能会导致类似问题

于 2013-05-23T18:08:25.060 回答