使用另一个子查询的别名

时间:2013-10-24 06:03:10

标签: subquery alias firebird

我有ff。查询

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

但该查询有错误列未知。 X.TBL_ID。即可。我想使用从第一个子查询到第二个子查询的WHERE子句的别名TBL_ID。

请帮我解决这个问题。

感谢!!! :)

1 个答案:

答案 0 :(得分: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移动到主查询的字段列表中。