[SQL Server]SQL行转列

图片 1

转换后

SELECT * FROM (
select ActionTargetType+actiontype as TypeResult, COUNT(RowGuid) as
Number from BanJianLogInfo group by ActionTargetType,actiontype
union
select ActionTargetType+OperateResult+’总数’ as TypeResult,count(*)
from BanJianLogInfo
group by ActionTargetType,OperateResult
union
select ActionTargetType+’总数’ as TypeResult,count(*) from
BanJianLogInfo group by ActionTargetType
)temp
PIVOT
( sum(Number) /*行转列后 列的值*/ FOR
temp.TypeResult/*急需行转列的列*/ IN
([办件音信成功总的数量],[办件新闻更新],[办件新闻失利总的数量],[办件新闻新扩充]/*列的值*/)
) AS T
转换前

图片 2

SELECT * FROM TableName temp /*亟需行转列数据源*/
PIVOT
( sum(ColumnName1) /*行转列后 列的值*/ FOR
temp.ColumnName2/*内需行转列的列*/ IN
([办件音信成功总量],[办件音讯更新],[办件消息战败总量],[办件音信新扩充]/*列的值*/)
) AS T

语法:

相关文章