Danielle Danielle - 1 year ago 46
SQL Question

Pivot tables - column alias not recognized in select statement

I am trying to produce a pivoted table. I have a picture of the query and data here.

I have aliased column names, since the columns are being aggregated.

Why isn't my alias usable?

Here is a picture of the error message.

Answer Source

Since you are aggregating the data in the Enrollment column via sum(Enrollment) that column is no longer available for the final select list. Remove it from the final select list and the query should work without any issues.

The thing to remember when using a PIVOT is columns you use inside of the statement are being replaced because you are converting the values into new columns. In the case of your query, both the Enrollment and ProgramType are not available in the final select.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download