You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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 COLUMNE1 instead of the correct column name COLUMN1 (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)

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:18:05