title: "Invalid column name" error when using subquery
body: <p>I have a query I've been running quarterly that pulls data regarding patients with certain medical problems who visit a clinic. The query has been running fine, but I was just asked to add a gender field so we could filter patients by male and female and look at any resulting patterns. The demographics table I've been using for the rest of the patient data already has a gender column, so I simply added it to my query, but when I go to run it, I get an "Invalid Column Name" error and I don't know why. </p>
  
  <p>Since I only need to display the Patient ID, Name, Visit Location, and now Gender, I've been using a subquery for the rest of the data I need to filter my patient list by. This is what I've got:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>As I mentioned, this query ran perfectly before I added the Gender field, and now I've got a red squiggle under the <code>[Patient: Gender]</code> portion of the initial <code>SELECT</code> statement and it's giving me the Invalid Column Name error. Any ideas why?</p>