我正在尝试从FROM子句中的另一个查询中选择记录。
Ex: SELECT * FROM (SELECT * FROM A WHERE A.COL_3=3) B WHERE B.COL_2!=1
FROM子句中的查询返回6条记录,其中A.COL_2的值为1.但是当B上的SELECT完成条件COL_2 NOT EQUAL为1时,我得到的相同记录是我在FROM子句中查询的COL_2的值为null。
这是在informix数据库上。
实际查询 -
SELECT * FROM
(
SELECT CASE NVL(A.CST_CD,-1)
WHEN -1 THEN (SELECT 1
FROM SOEVNT B
WHERE B.KNBR
= CAST(CONCAT(CAST(A.RNBR AS INTEGER),
CASE
WHEN A.RC_NBR >0 AND A.RC_NBR <10
THEN CONCAT('0000',A.RC_NBR)
WHEN A.RC_NBR >9 AND A.RC_NBR <100
THEN CONCAT('000',A.RC_NBR)
WHEN A.RC_NBR >99 AND A.RC_NBR <1000
THEN CONCAT('00',A.RC_NBR)
WHEN A.RC_NBR >999 AND A.RC_NBR <10000
THEN CONCAT('0',A.RC_NBR)
ELSE CAST(A.RC_NBR AS CHAR(5))
END) AS INTEGER)
AND B.MTE_EVNT = 'RG'
AND B.SO_LINE_NBR = D.DTL_SEQ_NBR)
ELSE 0
END AS SOEVNT_1,
CASE NVL(A.CST_CD,-1)
WHEN -1 THEN (SELECT 1
FROM SOEVNT B
WHERE B.KNBR = CAST(CONCAT(CAST(A.RNBR AS INTEGER),
CASE
WHEN A.RC_NBR >0 AND A.RC_NBR <10
THEN CONCAT('0000',A.RC_NBR)
WHEN A.RC_NBR >9 AND A.RC_NBR <100
THEN CONCAT('000',A.RC_NBR)
WHEN A.RC_NBR >99 AND A.RC_NBR <1000
THEN CONCAT('00',A.RC_NBR)
WHEN A.RC_NBR >999 AND A.RC_NBR <10000
THEN CONCAT('0',A.RC_NBR)
ELSE CAST(A.RC_NBR AS CHAR(5)) END)
AS INTEGER)
AND B.MTE_EVNT = 'K')
ELSE 0
END AS SOEVNT_2,
A.RNBR,
A.APTYP,
A.ORNBR,
A.RC_NBR,
NVL(A.CST_CD,-1) AS CSTYP,
CASE A.CST_CD WHEN 0 THEN 'YR2'
WHEN 1 THEN 'OSE' ELSE 'LEG' END AS ORDTYP,
D.DSNBR,
A.CTTS,
(SELECT PO_STAT_CD FROM STR_PO C
WHERE C.PO_CTRL_NBR = A.ORD_NBR AND C.APTYP = A.APTYP) AS POSTAT,
D.RQTY, A.DRETS
FROM SR A, OUTER (SRS D)
WHERE A.APTYP = 5
AND A.RTYP != 10
AND ( DATE(A.DRETS) > TODAY-20)
AND A.RC_NBR = D.RC_NBR AND D.RQTY > 0
) WHERE SOEVNT_1 != 1
单独执行时FROM子句中的查询结果 -
soevnt_1 soevnt_2 rnbr aptyp ornbr rc_nbr cstyp ordtyp dsnbr ctts postat rqty drets
1 NULL 0077 5 51003 79978 -1 LEG 1 NULL 3 1 2013-10-14 06:01:54.634
1 NULL 0077 5 51011 79966 -1 LEG 1 NULL 3 7 2013-10-03 05:21:06.493
1 NULL 0077 5 51012 79967 -1 LEG 1 NULL 3 5 2013-10-03 06:02:43.133
1 NULL 0077 5 51012 79981 -1 LEG 1 NULL 3 1 2013-10-17 14:39:25.295
1 NULL 0077 5 51013 79968 -1 LEG 1 NULL 3 1 2013-10-03 14:51:50.132
1 NULL 0077 5 51015 79969 -1 LEG 1 NULL 3 5 2013-10-04 03:59:09.835
执行完整查询时的结果集
soevnt_1 soevnt_2 rnbr aptyp ornbr rc_nbr cstyp ordtyp dsnbr ctts postat rqty drets
NULL NULL 0077 5 51003 79978 -1 LEG NULL NULL 3 NULL 2013-10-14 06:01:54.634
NULL NULL 0077 5 51011 79966 -1 LEG NULL NULL 3 NULL 2013-10-03 05:21:06.493
NULL NULL 0077 5 51012 79967 -1 LEG NULL NULL 3 NULL 2013-10-03 06:02:43.133
NULL NULL 0077 5 51012 79981 -1 LEG NULL NULL 3 NULL 2013-10-17 14:39:25.295
NULL NULL 0077 5 51013 79968 -1 LEG NULL NULL 3 NULL 2013-10-03 14:51:50.132
NULL NULL 0077 5 51015 79969 -1 LEG NULL NULL 3 NULL 2013-10-04 03:59:09.835
答案 0 :(得分:1)
这让我想起了informix 7.3中一个带有子查询的旧错误,它会忽略字段。
在您的特定查询中,我会尝试这样做: