为什么工作的SQL查询不能作为子查询工作?

时间:2012-07-12 13:32:06

标签: sql oracle10g subquery

我有以下查询,它可以生成我需要的输出。

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问题,但似乎没有人尝试过我(我希望有迹象表明我做错了什么)。对于我在这里缺少什么的建议?

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:1)

我最好的猜测是“*”会在子查询中引入两列名为“ref_no”的列,因为temp和cdec都有列。

尝试将选择更改为:

select cdec.*, temp.sequence_number

我之前在其他数据库中看到过这个错误,但信息更加清晰。