我有以下 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 表达式时如何解决这个问题。
任何帮助是极大的赞赏...
- 弗林