0

I have the ff. query

SELECT X.TBL_ID, X.TN, X.SN, X.FAIL, X.TEST_DESC, Y.TOTAL_ROW
FROM
    (SELECT
             A.ID AS TBL_ID,
             B.TEST_NUM AS TN,
             B.SITE_NUM AS SN,
             SUM(IIF(STATUS = 'FAIL', 1, 0)) FAIL,
     CASE
          WHEN (TEST_DESC CONTAINING 'OPEN') THEN 'OPEN'
          WHEN (TEST_DESC CONTAINING 'SHORT') THEN 'SHORT'
          WHEN (TEST_DESC CONTAINING 'Kelvin') THEN 'KELVIN'
          ELSE  'UNKNOWN'
      END AS TEST_DESC

      FROM T_TABLE2 B
      JOIN  T_TABLE1 A
      ON B.TBL1_ID = A.ID
         WHERE A.DEVICENAME = 'SAJO7A159E_P59'
         AND A.TESTER = 'T932-M2M-077'
         AND B.TEST_NUM = 'T201'
         OR B.TEST_NUM = 'T202'
         OR B.TEST_NUM = 'T203'
         OR B.TEST_NUM = 'T204'
         OR B.TEST_NUM = 'T205'
         OR B.TEST_NUM = 'T206'
         OR B.TEST_NUM = 'T207'
         OR B.TEST_NUM = 'T208'
         OR B.TEST_NUM = 'T209'
         OR B.TEST_NUM = 'T210'
         OR B.TEST_NUM = 'T211'
         OR B.TEST_NUM = 'T601'
         OR B.TEST_NUM = 'T602'
         OR B.TEST_NUM = 'T603'
      GROUP BY
         A.ID,
         B.TEST_NUM,
         B.SITE_NUM,
         B.TEST_DESC
      ORDER BY B.TEST_NUM) X,

      (SELECT (MAX(DEVICE) - MIN(DEVICE) - 1) AS TOTAL_ROW FROM T_TABLE3 WHERE TBL1_ID = X.TBL_ID) Y

but that query has an error Column unknown. X.TBL_ID.. I want to use the alias TBL_ID from first sub query to the WHERE clause of second sub query.

Please help me out of this.

THANKS!!! :)

4

1 回答 1

1

在我看来,你想要的是

SELECT X.TBL_ID, X.TN, X.SN, X.FAIL, X.TEST_DESC, 
   (SELECT (MAX(DEVICE) - MIN(DEVICE) - 1) FROM T_TABLE3 WHERE TBL1_ID = X.TBL_ID) as TOTAL_ROW
FROM
    (SELECT
    ...
    ) X

即将您的第二个查询 Y 移动到主查询的字段列表中。

于 2013-10-24T08:15:01.937 回答