使用别名会导致某些无效的标识符

时间:2018-07-20 09:22:32

标签: sql oracle alias

我正在尝试创建一个表格,列出患者的标识符,以用于在肿瘤发生前六个月内已将患有原发性原发肿瘤(MCC_IDS.TUMOURID)的患者诊断为患有另一种肿瘤(AV_TUMOUR.TUMOURID)的情况感兴趣。

这两个表中的列名称相同(MCC_IDS和AV_TUMOUR),因此我添加了一些列别名。 SQL Developer似乎不喜欢这样,因为我不断收到“无效标识符”错误。

我不确定还有什么尝试。救命!

    CREATE TABLE multitumours_hes_baseline
    AS
        ( SELECT mcc_ids.patientid,
                 mcc_ids.tumourid,
                 av_tumour.tumourid AS other_tumourid,
                 av_tumour.diagnosisdatebest AS other_diagnosisdatebest,
                 av_tumour.site_icd10_o2_3char AS other_icd10,
                 av_tumour.morph_icd10_o2 AS other_morph
            FROM mcc_ids left
          JOIN av2016.av_tumour ON mcc_ids.patientid = av_tumour.patientid -- Joining AV_TUMOUR onto MCC_IDS by PATIENTID to ensure that we can count TUMOURIDs other than those contained in MCC_IDS
--WHERE (AV_TUMOUR.OTHER_DATE>'31-DEC-2012' AND AV_TUMOUR.OTHER_DATE<'01-JAN-2016')
           WHERE mcc_ids.tumourid != av_tumour.other_tumourid
           AND ( mcc_ids.diagnosisdatebest >= av_tumour.other_diagnosisdatebest ) -- Only those conditions that occur in HES before the date of cancer diagnosis
           AND ( mcc_ids.diagnosisdatebest - av_tumour.other_diagnosisdatebest ) <= 182.5 -- Only those conditions that occur up to six months prior to the date of diagnosis
           AND av_tumour.dedup_flag = 1
        );

1 个答案:

答案 0 :(得分:3)

您不能在同一级别的查询中引用列别名(在order-by子句中除外),但是您无需在此处引用。

您正在做

WHERE MCC_IDS.TUMOURID!=AV_TUMOUR.OTHER_TUMOURID
AND (MCC_IDS.DIAGNOSISDATEBEST>=AV_TUMOUR.OTHER_DIAGNOSISDATEBEST)
AND (MCC_IDS.DIAGNOSISDATEBEST-AV_TUMOUR.OTHER_DIAGNOSISDATEBEST)<=182.5

OTHER_是结果集中的列别名,因此即使您可以使用它们,它们也不会作为原始表中的列存在,这就是AV_TUMOUR.OTHER_*试图做到的。找。使用实际的列名称:

WHERE MCC_IDS.TUMOURID!=AV_TUMOUR.TUMOURID
AND MCC_IDS.DIAGNOSISDATEBEST>=AV_TUMOUR.DIAGNOSISDATEBEST
AND MCC_IDS.DIAGNOSISDATEBEST-AV_TUMOUR.DIAGNOSISDATEBEST<=182.5

并且您不能在没有表前缀的情况下引用列别名-即WHERE MCC_IDS.TUMOURID!=OTHER_TUMOURID-因为它是相同级别的查询,并且是由于内部解析查询的方式; from the docs

  

c_alias
  指定列表达式的别名。 ...在查询期间,别名有效地重命名了选择列表项。别名可以在 order_by_clause 中使用,但不能在查询中的其他子句中使用。


也没有什么关系,但是要追溯六个月,您可以:

LEFT JOIN AV_TUMOUR ON AV_TUMOUR.PATIENTID = MCC_IDS.PATIENTID
WHERE AV_TUMOUR.OTHER_TUMOURID != MCC_IDS.TUMOURID
AND AV_TUMOUR.OTHER_DIAGNOSISDATEBEST >= ADD_MONTHS(MCC_IDS.DIAGNOSISDATEBEST, -6)
AND AV_TUMOUR.DEDUP_FLAG=1

您使用的是左外部联接,但由于WHERE子句中也有条件,因此实际上会将其转换为内部联接。除非您不想在那个时期看到{<1>} 没有其他肿瘤的MCC_IDS,否则可以删除“ LEFT”:

JOIN AV_TUMOUR ON AV_TUMOUR.PATIENTID = MCC_IDS.PATIENTID
WHERE AV_TUMOUR.OTHER_TUMOURID != MCC_IDS.TUMOURID
AND AV_TUMOUR.OTHER_DIAGNOSISDATEBEST >= ADD_MONTHS(MCC_IDS.DIAGNOSISDATEBEST, -6)
AND AV_TUMOUR.DEDUP_FLAG=1

如果您确实希望查看这些内容(从您的描述来看似乎不太可能),则将所有条件纳入ON中:

LEFT JOIN AV_TUMOUR ON AV_TUMOUR.PATIENTID = MCC_IDS.PATIENTID
AND AV_TUMOUR.OTHER_TUMOURID != MCC_IDS.TUMOURID
AND AV_TUMOUR.OTHER_DIAGNOSISDATEBEST >= ADD_MONTHS(MCC_IDS.DIAGNOSISDATEBEST, -6)
AND AV_TUMOUR.DEDUP_FLAG=1