如何在SQL中转置文本列?行转列查询报错求助
Fixing Your SQL PIVOT Query
Let's get that pivot working correctly for you! I noticed a handful of small issues in your original code that are triggering the error:
- Spelling mistake: You wrote
COLUMNE1instead of the correct column nameCOLUMN1(extra 'e' slipped in there!) - Syntax errors in the IN clause:
- The column names inside
IN()don't need single quotes around them - You accidentally added a colon after
LastName(LastName:) which is invalid - The extra brackets around
[Address]in the IN clause are unnecessary (though if you keep them, don't wrap them in quotes)
- The column names inside
Here's the corrected query that should work as expected:
SELECT pt.EmployeeID, pt.FirstName, pt.LastName, pt.[Address] FROM ( -- Only select the columns we need for pivoting to avoid clutter SELECT COLUMN1, COLUMN2 FROM FlatFile.UserList ) AS temp PIVOT ( MAX(COLUMN2) FOR COLUMN1 IN (EmployeeID, FirstName, LastName, [Address]) ) AS pt
A quick note: I also updated the subquery to only pull COLUMN1 and COLUMN2 instead of using SELECT *—this is a good practice because it ensures we only include the data necessary for the pivot, which prevents unexpected behavior if the source table ever gets additional columns.
内容的提问来源于stack exchange,提问作者krunal Patel
相关产品推荐
相关产品推荐

