且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用带有SQL的数据透视表将某些列转置为行

更新时间:2023-11-18 21:55:16

您的查询有一些错误.

首先,您在PIVOT上缺少聚合函数.您需要围绕propertyvalue的汇总.

First, you are missing an aggregate function on your PIVOT. You need an aggregate around propertyvalue.

第二,您需要用方括号而不是单引号括住$row1等.

Second, you need to surround the $row1, etc with square brackets not single quotes.

第三,我将为as pivot

因此,代码将是:

select * 
from 
(
  select name, propertyvalue, displayname
  from indexrows
) a
pivot
(
  max(propertyvalue)
  for [displayname] in ([$row1], [$row2], [$row3])
) piv;

请参见带有演示的SQL提琴