不确定为啥会出现 ORA-00918 列定义不明确

Posted

技术标签:

【中文标题】不确定为啥会出现 ORA-00918 列定义不明确【英文标题】:Unsure why ORA-00918 column ambiguously defined is appearing不确定为什么会出现 ORA-00918 列定义不明确 【发布时间】:2019-08-12 13:11:17 【问题描述】:

我不确定为什么在输入以下代码时会出现 ORA-00918 错误消息。

我看不出哪一列的定义不明确。

我要做的是创建一个表,该表根据查询 A 和 B 匹配中的 work_header_no、work_version_no 和 site_numbers 匹配来提取 b.site_code 值。

代码如下

SELECT 

a.statement.statement_date,
a.sw_header.organise_code,
a.organisation.organise_name,
a.PermitRef,
a.actual_inspection.logged_time,
a.insp_category.insp_category_name,
a.actual_inspection.insp_number,
a.actual_inspection.site_number,
a.inspection_outcome.insp_outcome_name,
a.insp_category.insp_charge,
a.actual_inspection.insp_notes,
a.actual_inspection.work_header_no,
a.actual_inspection.insp_time,

b.site_code

FROM 

(select
statement.statement_date,
sw_header.organise_code,
organisation.organise_name,
CAST(
organisation.external_ref_2 ||''||
sw_header.works_ref||'.'||
sw_notice_header.app_seq_no||'.'||
sw_notice_header.ext_version_no 
as VARCHAR (40)) as PermitRef,
actual_inspection.logged_time,
insp_category.insp_category_name,
actual_inspection.insp_number,
actual_inspection.site_number,
inspection_outcome.insp_outcome_name,
insp_category.insp_charge,
actual_inspection.insp_notes,
actual_inspection.work_header_no,
actual_inspection.insp_time,
sw_notice_header.work_header_no,
sw_notice_header.work_version_no,
actual_inspection.site_number

from
actual_inspection
inner join sw_header on 
actual_inspection.work_header_no = sw_header.work_header_no

inner join sw_notice_header on 
sw_header.work_header_no = sw_notice_header.work_header_no 
and sw_header.work_version_no = sw_notice_header.work_version_no

inner join insp_category on 
actual_inspection.insp_category_code = insp_category.insp_category_code

inner join inspection_outcome on 
actual_inspection.insp_outcome_code = inspection_outcome.insp_outcome_code

inner join organisation on 
sw_header.organise_code = organisation.organise_code

inner join statement on 
organisation.organise_code = statement.organise_code 
and organisation.statement_number = statement.statement_no


where 
actual_inspection.notice_type_code = '2600' and
actual_inspection.insp_outcome_code != 'O40' 


order by
actual_inspection.logged_time)

a

   JOIN

(
select
sns.work_header_no,
sns.work_version_no,
sns.site_number,
sns.site_code

from
sw_notice_site sns
)

b

ON a.work_header_no = b.work_header_no and
a.work_version_no = b.work_version_no and
a.site_number = b.site_number

【问题讨论】:

【参考方案1】:

您有重复的actual_inspection.site_numberwork_header_no 删除重复的行

actual_inspection.site_number,
inspection_outcome.insp_outcome_name,
insp_category.insp_charge,
actual_inspection.insp_notes,
actual_inspection.work_header_no,
actual_inspection.insp_time,
sw_notice_header.work_header_no,
sw_notice_header.work_version_no,
actual_inspection.site_number

【讨论】:

谢谢。已经这样做了(不敢相信我错过了!)但仍然出现相同的错误消息。 @RobMorris 也删除 actual_inspection.work_header_no, 谢谢。已删除这些并在顶部选择语句中取出表名,使其为 a.site_number、a.insp_outcome_name 等,这已经奏效了。【参考方案2】:

无需使用a.statement.statement_date。您可以使用a.statement_date。 同样,将所有其他列更改为 a.

【讨论】:

【参考方案3】:

您的整个查询应如下所示:

SELECT -- removed table names from all the columns
    A.STATEMENT_DATE,
    A.ORGANISE_CODE,
    A.ORGANISE_NAME,
    A.PERMITREF,
    A.LOGGED_TIME,
    A.INSP_CATEGORY_NAME,
    A.INSP_NUMBER,
    A.SITE_NUMBER,
    A.INSP_OUTCOME_NAME,
    A.INSP_CHARGE,
    A.INSP_NOTES,
    A.WORK_HEADER_NO,
    A.INSP_TIME,
    B.SITE_CODE
FROM
    (
        SELECT
            STATEMENT.STATEMENT_DATE,
            SW_HEADER.ORGANISE_CODE,
            ORGANISATION.ORGANISE_NAME,
            CAST(ORGANISATION.EXTERNAL_REF_2
                 || ''
                 || SW_HEADER.WORKS_REF
                 || '.'
                 || SW_NOTICE_HEADER.APP_SEQ_NO
                 || '.'
                 || SW_NOTICE_HEADER.EXT_VERSION_NO AS VARCHAR(40)) AS PERMITREF,
            ACTUAL_INSPECTION.LOGGED_TIME,
            INSP_CATEGORY.INSP_CATEGORY_NAME,
            ACTUAL_INSPECTION.INSP_NUMBER,
            --ACTUAL_INSPECTION.SITE_NUMBER, -- commented this as it is there in statement twice
            INSPECTION_OUTCOME.INSP_OUTCOME_NAME,
            INSP_CATEGORY.INSP_CHARGE,
            ACTUAL_INSPECTION.INSP_NOTES,
            ACTUAL_INSPECTION.WORK_HEADER_NO,
            ACTUAL_INSPECTION.INSP_TIME,
            --SW_NOTICE_HEADER.WORK_HEADER_NO, -- commented this as it is there in statement twice
            SW_NOTICE_HEADER.WORK_VERSION_NO,
            ACTUAL_INSPECTION.SITE_NUMBER
        FROM
            ACTUAL_INSPECTION
            INNER JOIN SW_HEADER ON ACTUAL_INSPECTION.WORK_HEADER_NO = SW_HEADER.WORK_HEADER_NO
            INNER JOIN SW_NOTICE_HEADER ON SW_HEADER.WORK_HEADER_NO = SW_NOTICE_HEADER.WORK_HEADER_NO
                                           AND SW_HEADER.WORK_VERSION_NO = SW_NOTICE_HEADER.WORK_VERSION_NO
            INNER JOIN INSP_CATEGORY ON ACTUAL_INSPECTION.INSP_CATEGORY_CODE = INSP_CATEGORY.INSP_CATEGORY_CODE
            INNER JOIN INSPECTION_OUTCOME ON ACTUAL_INSPECTION.INSP_OUTCOME_CODE = INSPECTION_OUTCOME.INSP_OUTCOME_CODE
            INNER JOIN ORGANISATION ON SW_HEADER.ORGANISE_CODE = ORGANISATION.ORGANISE_CODE
            INNER JOIN STATEMENT ON ORGANISATION.ORGANISE_CODE = STATEMENT.ORGANISE_CODE
                                    AND ORGANISATION.STATEMENT_NUMBER = STATEMENT.STATEMENT_NO
        WHERE
            ACTUAL_INSPECTION.NOTICE_TYPE_CODE = '2600'
            AND ACTUAL_INSPECTION.INSP_OUTCOME_CODE != 'O40'
        ORDER BY
            ACTUAL_INSPECTION.LOGGED_TIME
    ) A
    JOIN (
        SELECT
            SNS.WORK_HEADER_NO,
            SNS.WORK_VERSION_NO,
            SNS.SITE_NUMBER,
            SNS.SITE_CODE
        FROM
            SW_NOTICE_SITE SNS
    ) B ON A.WORK_HEADER_NO = B.WORK_HEADER_NO
           AND A.WORK_VERSION_NO = B.WORK_VERSION_NO
           AND A.SITE_NUMBER = B.SITE_NUMBER;

干杯!!

【讨论】:

以上是关于不确定为啥会出现 ORA-00918 列定义不明确的主要内容,如果未能解决你的问题,请参考以下文章

Oracle Pivot 和 Pivot XML - ORA-00918:列定义不明确

ORA-00918: 列定义不明确,使用 DB Link

ORA-00918: 列定义不明确

ORA-00918: 使用数据透视时列定义不明确

Oracle Live SQL 中的错误 -> ORA-00918:列定义不明确

ORA-00918: FOR pharmacyschema.sql (RX 药房数据库) 的列定义不明确