使用子查询时出现“无效的列名称”错误
我有一个查询,我一直在每季度运行一次,查询有关访问诊所的某些医疗问题的患者的数据。查询运行良好,但我刚刚被要求添加一个性别字段,以便我们可以按照男性和女性过滤患者,并查看任何生成的模式。我已经用于其他患者数据的人口统计表已经有一个性别列,所以我只是将它添加到我的查询中,但是当我运行它时,出现“无效列名”错误,我不知道不知道为什么。使用子查询时出现“无效的列名称”错误
由于我只需要显示患者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]
部分下有一个红色的波浪线,它的给我的无效列名称错误。任何想法为什么?
更新:我回答我自己的问题,因为我找到了问题。在我的子查询中,我有d.[Patient: Gender] as [Gender]
在我的SELECT
声明中(我通常会重命名为这样的列,因为这个数据库中的很多表有这么长的标题,使得我的列不必要的宽和丑)。因此,当我试图在我的主查询中选择相同的[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
你可能认为该列在人口统计表中,但它不是。
最可能的原因(在这种情况下)是拼写错误。如果我不得不猜测,那么在'性别'之前或之后名称中会有更多空格。找到列的实际名称
的方法之一是看在information_schema.columns
:
select '|' + column_name + '|'
from information_schema.columns
where table_name = 'Demographics' and
lower(column_name) like '%gender%';
刚刚尝试过 - 我使用的是正确的 – EJF
如果命名FROM表如T1应该命名在选择列根据withn本(DINAMIC)表名
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
内部查询是否独立工作?从[患者:性别]人口统计组中选择count(1),[Patient:Gender]是否返回2条记录? – xQbert
从技术上讲,由于系统允许“男性”,“女性”,“其他”和“未知”,所以它返回4条记录,但除此之外,这是有效的。 – EJF
好的,所以我们知道列名是正确的......将外部查询的别名更改为“oDemographics”,可能会将其别名为与现有表相同的名称导致问题。 (并做了内部查询本身的工作?) – xQbert