2012-07-12 188 views
0

我有以下查詢,它工作併產生我需要它的輸出。爲什麼工作的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問題,但沒有似乎想要我什麼(我期望的指示,我做錯了什麼)。對於我在這裏失蹤的任何建議?

+0

請發佈完整的查詢給出問題。 – RedFilter 2012-07-12 13:38:55

+0

對此有一個downvote,有人可以告訴我爲什麼這樣我可以避免重複錯誤?謝謝。 – 2012-07-12 13:50:36

+0

在最後一個查詢中,您有「SELECT resolved。*,fdec.decision_code initial_decision」。 initial_decision應該是什麼?我假設它不是列名,因爲我沒有在其他地方看到它,並且之前沒有逗號。從來沒有使用甲骨文,我從來沒有見過這麼好奇之前,它意味着什麼/用於。 – Bryan 2012-07-12 14:06:18

回答

2

的問題是在這裏:

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 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 

您選擇ref_no兩次(一次從temp,一旦因爲*cdec),所以它不知道哪個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 temp.ref_year, temp.ref_no, cdec.decision_code 
    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 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; 
+0

現在完整查詢,謝謝。 SQL開發人員建議所有括號都匹配。 – 2012-07-12 13:44:17

+0

完美,謝謝。 – 2012-07-12 14:16:27

1

我最好的猜測是,「*」在名爲「ref_no」成子查詢兩列帶來的,因爲溫度和CDEC都有列。

嘗試改變選擇:

select cdec.*, temp.sequence_number 

我以前見過這個錯誤在其他數據庫,但該消息表現得更加明顯。