次のクエリがあります。これは機能し、必要な出力を生成します。
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の質問を見てきましたが、私が何をしようとしているようには見えません(何か間違ったことをしている兆候を期待しています)。ここで欠けているものについて何か提案はありますか?