Home >Database >Mysql Tutorial >数据透视

数据透视

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-07 14:57:471428browse

对数据集进行透视 无 DECLARE @sql NVARCHAR(2000)SELECT @sql=ISNULL(@sql+',','')+'['+FEEDATE+']' FROM TEMPTB GROUP BY FEEDATE PRINT @sql--注意,pivot透视中的in后面的条件中,只能是以字符开始,如:汉字、字母;--上面的FEEDATE即使设置为varchar类型,

对数据集进行透视
DECLARE
    @sql NVARCHAR(2000)
SELECT @sql=ISNULL(@sql+',','')+'['+FEEDATE+']'  FROM TEMPTB GROUP BY FEEDATE    
PRINT @sql
--注意,pivot透视中的in后面的条件中,只能是以字符开始,如:汉字、字母;
--上面的FEEDATE即使设置为varchar类型,但如果其内容为20121101,1,2等数字,放在下面的IN当中仍然会出错
--所以,在数字外面包括了一层[FEEDATE]
SET @sql='SELECT * FROM TEMPTB PIVOT(MAX(TOTALFEE) FOR FEEDATE IN('+@sql+'))A'
EXEC(@sql)
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn