2016-11-30 83 views
0

我有一個查詢,我一直在每季度運行一次,查詢有關訪問診所的某些醫療問題的患者的數據。查詢運行良好,但我剛剛被要求添加一個性別字段,以便我們可以按照男性和女性過濾患者,並查看任何生成的模式。我已經用於其他患者數據的人口統計表已經有一個性別列,所以我只是將它添加到我的查詢中,但是當我運行它時,出現「無效列名」錯誤,我不知道不知道爲什麼。使用子查詢時出現「無效的列名稱」錯誤

由於我只需要顯示患者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]部分下有一個紅色的波浪線,它的給我的無效列名稱錯誤。任何想法爲什麼?

+0

內部查詢是否獨立工作?從[患者:性別]人口統計組中選擇count(1),[Patient:Gender]是否返回2條記錄? – xQbert

+1

從技術上講,由於系統允許「男性」,「女性」,「其他」和「未知」,所以它返回4條記錄,但除此之外,這是有效的。 – EJF

+0

好的,所以我們知道列名是正確的......將外部查詢的別名更改爲「oDemographics」,可能會將其別名爲與現有表相同的名稱導致問題。 (並做了內部查詢本身的工作?) – xQbert

回答

0

更新:我回答我自己的問題,因爲我找到了問題。在我的子查詢中,我有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 
0

你可能認爲該列在人口統計表中,但它不是。

最可能的原因(在這種情況下)是拼寫錯誤。如果我不得不猜測,那麼在'性別'之前或之後名稱中會有更多空格。找到列的實際名稱

的方法之一是看在information_schema.columns

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

剛剛嘗試過 - 我使用的是正確的 – EJF

0

如果命名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 
相關問題