参照这个网址介绍
http://www.cnblogs.com/lwhkdash/archive/2012/06/26/2562979.html
一般SQL Server的函数都会识别为紫色,可是PIVOT却是灰色,我一开始以为是SQL Server不能使用,但能正常使用
可以使用print @@version确认下兼容级别,90的是没问题的
Microsoft SQL Server 2005 - 9.00.4060.00 (X64)
数据库右键->属性->选项
这聚合函数对text,nvarchar,varchar字段都不适用,只有将INCOME是DECIMAL才可以
create table WEEK_INCOME3(WEEK NVARCHAR(10),INCOME text)
既然需要1个函数,那对于varchar类型还可以用max函数作变通的办法,text就还是不行
create table WEEK_INCOME4(WEEK NVARCHAR(10),INCOME VARCHAR(10))
delete from WEEK_INCOME4
INSERT INTO WEEK_INCOME4
SELECT 'D1', '$1000'
UNION ALL
SELECT'D2', '$2000'
UNION ALL
SELECT'D3', '$3000'
UNION ALL
SELECT'D4', '$4000'
UNION ALL
SELECT'D5', '$5000'
UNION ALL
SELECT'D6', '$6000'
UNION ALL
SELECT'D7', '$7000'
由PIVOT函数的格式可以看出,WEEK column的值作为变化后的列名,不是字符串的形式,若是column值有空格,则需要用中括号。
update WEEK_INCOME4 set WEEK ='D1 D1' where iNCOME='$1000'
select * from WEEK_INCOME4
pivot(max(INCOME) for [WEEK] in ([D1 D1],D2,D3,D4,D5,D6,D7)) tbl