我有以下查询,它可以工作并产生我需要的输出。
SELECT
*
FROM
(SELECT
ref_year, ref_no, min(sequence_number) sequence_number
FROM
case_decisions
WHERE
stage = 'C'
AND
decision_code <> 'IA'
GROUP BY
ref_year,
ref_no
) temp
INNER JOIN /* Produce table of first decisions only */
case_decisions cdec
ON
temp.ref_year = cdec.ref_year
AND
temp.ref_no = cdec.ref_no
AND
temp.sequence_number = cdec.sequence_number;
但是,如果我尝试将它用作另一个工作查询的子查询连接(如下所示),我会收到一条错误消息,指出 fdec.ref_no 不是有效标识符。我认为我编写子查询的方式存在问题,但找不到它。
LEFT OUTER JOIN
(SELECT
*
FROM
(
SELECT
ref_year, ref_no, min(sequence_number) sequence_number
FROM
case_decisions
WHERE
stage = 'C'
AND
decision_code <> 'IA'
GROUP BY
ref_year,
ref_no
) temp
INNER JOIN /* Produce table of first decisions only */
case_decisions cdec
ON
temp.ref_year = cdec.ref_year
AND
temp.ref_no = cdec.ref_no
AND
temp.sequence_number = cdec.sequence_number
) fdec
ON
resolved.ref_year = fdec.ref_year
AND
resolved.ref_no = fdec.ref_no
给出一个完整的查询:
SELECT
resolved.*,
fdec.decision_code initial_decision
FROM
(SELECT
t1.ref_year,
t1.ref_no,
t1.date_decision_status_achieved,
t1.sequence_number
FROM
case_decisions t1
LEFT OUTER JOIN
case_decisions t2
ON
(t1.ref_year = t2.ref_year
AND
t1.ref_no = t2.ref_no
AND
t1.sequence_number < t2.sequence_number)
WHERE
t2.ref_year IS NULL
AND
t2.ref_no IS NULL
AND
(t1.status = 8 OR t1.status =11)
AND
t1.decision_code <> 'IA'
AND
t1.date_decision_status_achieved between '01-APR-2008' and '01-JUN-2008' /* dates for testing purposes only */) resolved
LEFT OUTER JOIN
(SELECT
*
FROM
(SELECT
ref_year, ref_no, min(sequence_number) sequence_number
FROM
case_decisions
WHERE
stage = 'C'
AND
decision_code <> 'IA'
GROUP BY
ref_year,
ref_no
) temp
INNER JOIN /* Produce table of first decisions only */
case_decisions cdec
ON
temp.ref_year = cdec.ref_year
AND
temp.ref_no = cdec.ref_no
AND
temp.sequence_number = cdec.sequence_number) fdec
ON
resolved.ref_year = fdec.ref_year
AND
resolved.ref_no = fdec.ref_no
;
我看过类似的 SO 问题,但似乎没有人在尝试我是什么(我希望有迹象表明我做错了)。对于我在这里缺少的东西有什么建议吗?