且构网

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

使用数据透视和 SQL 将一些列转置为行

更新时间:2023-11-18 22:03:52

您的查询有一些问题.

首先,您的 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 Fiddle with Demo