[SQL Server]SQL行转列

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
转换前转换后语法:SELECT * FROM TableName temp /*需要行转列数据源*/
PIVOT
( sum(ColumnName1) /*行转列后 列的值*/ FOR
temp.ColumnName2/*需要行转列的列*/ IN ([办件信息成功总数],[办件信息更新],[办件信息失败总数],[办件信息新增]/*列的值*/)
) AS T

相关内容推荐