我有以下查詢,它工作併產生我需要它的輸出。爲什麼工作的SQL查詢不能用作子查詢?
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問題,但沒有似乎想要我什麼(我期望的指示,我做錯了什麼)。對於我在這裏失蹤的任何建議?
請發佈完整的查詢給出問題。 – RedFilter 2012-07-12 13:38:55
對此有一個downvote,有人可以告訴我爲什麼這樣我可以避免重複錯誤?謝謝。 – 2012-07-12 13:50:36
在最後一個查詢中,您有「SELECT resolved。*,fdec.decision_code initial_decision」。 initial_decision應該是什麼?我假設它不是列名,因爲我沒有在其他地方看到它,並且之前沒有逗號。從來沒有使用甲骨文,我從來沒有見過這麼好奇之前,它意味着什麼/用於。 – Bryan 2012-07-12 14:06:18