Sql &引用;无效的列名";使用子查询时出错

Sql &引用;无效的列名";使用子查询时出错,sql,sql-server,subquery,Sql,Sql Server,Subquery,我有一个查询,我每季度都会运行一次,它会收集到一些有特定医疗问题的患者到诊所就诊的数据。这个查询一直运行良好,但我只是被要求添加一个性别字段,以便我们可以按男性和女性筛选患者,并查看任何结果模式。我用于其余患者数据的人口统计表已经有一个性别列,所以我只是将其添加到查询中,但当我运行它时,会出现“无效列名”错误,我不知道为什么 由于我只需要显示患者ID、姓名、就诊位置,现在还需要显示性别,因此我一直在使用子查询来筛选患者列表所需的其余数据。这就是我得到的: SELECT DISTINCT [Med

我有一个查询,我每季度都会运行一次,它会收集到一些有特定医疗问题的患者到诊所就诊的数据。这个查询一直运行良好,但我只是被要求添加一个性别字段,以便我们可以按男性和女性筛选患者,并查看任何结果模式。我用于其余患者数据的人口统计表已经有一个性别列,所以我只是将其添加到查询中,但当我运行它时,会出现“无效列名”错误,我不知道为什么

由于我只需要显示患者ID、姓名、就诊位置,现在还需要显示性别,因此我一直在使用子查询来筛选患者列表所需的其余数据。这就是我得到的:

SELECT DISTINCT
[MedHist: Patient ID] as [Patient ID],
[Patient: First Last Name] as [Patient Name],
[Patient: Gender] as [Gender],
ServiceLocationID as [Service Location]

FROM
    (SELECT DISTINCT
    mh.[MedHist: Patient ID],
    d.[Patient: First Last Name],
    d.[Patient: Date of Birth],
    d.[Patient: Gender] as [Gender],
    d.[Patient: Age],
    a.Status,
    mh.[MedHist: Procedure Code],
    pm.Description,
    v.ServiceLocationID

    FROM MedicalHistory mh INNER JOIN Demographics d ON mh.[MedHist: Patient ID] = d.[Patient: Patient ID]
        INNER JOIN Appointment a ON a.PatientID = d.[Patient: Patient ID]
        JOIN Visit v ON v.PatientID = d.[Patient: Patient ID]
        JOIN PatientMeds pm ON pm.PatientID = d.[Patient: Patient ID]

    WHERE d.[Patient: Age] ...is in a certain range
        AND a.Status ...is a certain thing
        AND pm.Description ...involves a certain medication
        AND some other stuff

    ) Demographics
正如我提到的,在我添加性别字段之前,这个查询运行得非常好,现在我在初始
SELECT
语句的
[Patient:Gender]
部分下面有一个红色的曲线,它给了我一个无效的列名错误。你知道为什么吗?

你可能认为该列在人口统计表中,但事实并非如此

最可能的原因(在本例中)是拼写错误。如果非要我猜的话,名字中有额外的空格,在“性别”之前或之后

查找列的实际名称的一种方法是查看
信息\u架构。列

select '|' + column_name + '|'
from information_schema.columns
where table_name = 'Demographics' and
      lower(column_name) like '%gender%';

如果从表eg t1命名,则应根据此(dinamic)表名在select中命名列

  SELECT DISTINCT
  [t1: Patient ID] as [Patient ID],
  [t1: First Last Name] as [Patient Name],
  [t1: Gender] as [Gender],
  [t1: ServiceLocationID as [Service Location]

  FROM
      (SELECT DISTINCT
      mh.[MedHist: Patient ID],
      d.[Patient: First Last Name],
      d.[Patient: Date of Birth],
      d.[Patient: Gender],
      d.[Patient: Age],
      a.Status,
      mh.[MedHist: Procedure Code],
      pm.Description,
      v.ServiceLocationID

      FROM MedicalHistory mh INNER JOIN Demographics d ON mh.[MedHist: Patient ID] = d.[Patient: Patient ID]
          INNER JOIN Appointment a ON a.PatientID = d.[Patient: Patient ID]
          JOIN Visit v ON v.PatientID = d.[Patient: Patient ID]
          JOIN PatientMeds pm ON pm.PatientID = d.[Patient: Patient ID]

      WHERE d.[Patient: Age] ...is in a certain range
          AND a.Status ...is a certain thing
          AND pm.Description ...involves a certain medication
          AND some other stuff

      ) t1

更新:我在回答我自己的问题,因为我解决了这个问题

在我的子查询中,我在
SELECT
语句中将
d.[Patient:Gender]作为[Gender]
(我通常这样重命名列,因为此数据库中的许多表都有很长的标题,这使我的列变得不必要的宽和丑)。因此,当我尝试在主查询中选择相同的
[Patient:Gender]
字段时,系统无法找到它,因为它已在子查询中重命名。以下代码起作用:

SELECT DISTINCT
    [MedHist: Patient ID] as [Patient ID],
    [Patient: First Last Name] as [Patient Name],
    Gender,
    ServiceLocationID as [Service Location]

FROM
    (SELECT DISTINCT
    mh.[MedHist: Patient ID],
    d.[Patient: First Last Name],
    d.[Patient: Date of Birth],
    d.[Patient: Gender] as [Gender],
    d.[Patient: Age],
    a.Status,
    mh.[MedHist: Procedure Code],
    .....and so on

内部查询本身工作吗?
是否通过[Patient:Gender]从人口统计组中选择count(1),[Patient:Gender]返回2条记录?从技术上讲,它返回4条记录,因为系统允许“男性”、“女性”、“其他”和“未知”,但除此之外,是的,这是有效的。好的,因此我们知道列名是正确的。。。将外部查询的别名更改为
oDemographics
,可能将其别名为与现有表相同的名称会导致问题。(内部查询本身是否工作?)因此更改外部查询的别名没有帮助,而且是的,内部查询本身的工作效率很低。我能想到的另一件事是尝试将内部查询字段
[Patient:Gender]
别名为其他内容,并引用外部查询字段上的新别名。。。如果真是这样,我也不知道为什么。