[SQL Server]SQL行转列
程序员文章站
2022-06-29 17:28:32
SELECT * FROM (select ActionTargetType+actiontype as TypeResult, COUNT(RowGuid) as Number from BanJianLogInfo group by ActionTargetType,actiontype uni ......
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
推荐阅读
-
SQL Server客户端工具到底使用的是哪个provider呢?
-
从SQL server数据库导入Mysql数据库的体验
-
SQL SERVER 查询性能优化分析事务与锁(三)
-
Oracle 与 SQL Server的简单常用的插入语句的区别
-
微软为PHP添加SQL Server LocalDb及SQL Server AlwaysOn集群支持
-
SQL Server 任务监控脚本
-
(Sql Server)数据的拆分和合并
-
Delphi XE 10.1 Berlin 使用FireDAC连接数据库(SQL Server, MySQL, SQLite)
-
SQL Server 2008 能用机器名连接,不能用IP地址连接问题 的一个
-
一个完整的SQL SERVER数据库全文索引的示例