0

我有以下查询,它可以工作并产生我需要的输出。

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

4

2 回答 2

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;
于 2012-07-12T13:34:58.973 回答
1

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

尝试将选择更改为:

select cdec.*, temp.sequence_number

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

于 2012-07-12T13:39:47.337 回答