一个简单的SQL 行列转换语句

时间:2024-01-24 01:57:08 

一个简单的SQL 行列转换
Author: eaglet
在数据库开发中经常会遇到行列转换的问题,比如下面的问题,部门,员工和员工类型三张表,我们要统计类似这样的列表
部门编号 部门名称 合计 正式员工 临时员工 辞退员工
1 A 30 20 10 1
这种问题咋一看摸不着头绪,不过把思路理顺后再看,本质就是一个行列转换的问题。下面我结合这个简单的例子来实现行列转换。
下面3张表


if exists ( select * from sysobjects where id = object_id ( ' EmployeeType ' ) and type = ' u ' )
drop table EmployeeType
GO
if exists ( select * from sysobjects where id = object_id ( ' Employee ' ) and type = ' u ' )
drop table Employee
GO
if exists ( select * from sysobjects where id = object_id ( ' Department ' ) and type = ' u ' )
drop table Department
GO
create table Department
(
Id int primary key ,
Department varchar ( 10 )
)
create table Employee
(
EmployeeId int primary key ,
DepartmentId int Foreign Key (DepartmentId) References Department(Id) , -- DepartmentId ,
EmployeeName varchar ( 10 )
)
create table EmployeeType
(
EmployeeId int Foreign Key (EmployeeId) References Employee(EmployeeId) , -- EmployeeId ,
EmployeeType varchar ( 10 )
)


描述部门,员工和员工类型之间的关系。
插入测试数据


insert Department values ( 1 , ' A ' );
insert Department values ( 2 , ' B ' );
insert Employee values ( 1 , 1 , ' Bob ' );
insert Employee values ( 2 , 1 , ' John ' );
insert Employee values ( 3 , 1 , ' May ' );
insert Employee values ( 4 , 2 , ' Tom ' );
insert Employee values ( 5 , 2 , ' Mark ' );
insert Employee values ( 6 , 2 , ' Ken ' );
insert EmployeeType values ( 1 , ' 正式 ' );
insert EmployeeType values ( 2 , ' 临时 ' );
insert EmployeeType values ( 3 , ' 正式 ' );
insert EmployeeType values ( 4 , ' 正式 ' );
insert EmployeeType values ( 5 , ' 辞退 ' );
insert EmployeeType values ( 6 , ' 正式 ' );


看一下部门、员工和员工类型的列表
Department EmployeeName EmployeeType
---------- ------------ ------------
A Bob 正式
A John 临时
A May 正式
B Tom 正式
B Mark 辞退
B Ken 正式
现在我们需要输出这样一个列表
部门编号 部门名称 合计 正式员工 临时员工 辞退员工
这个问题我的思路是首先统计每个部门的员工类型总数
这个比较简单,我把它做成一个视图


if exists ( select * from sysobjects where id = object_id ( ' VDepartmentEmployeeType ' ) and type = ' v ' )
drop view VDepartmentEmployeeType
GO
create view VDepartmentEmployeeType
as
select Department.Id, Department.Department, EmployeeType.EmployeeType, count (EmployeeType.EmployeeType) Cnt
from Department, Employee, EmployeeType where
Department.Id = Employee.DepartmentId and Employee.EmployeeId = EmployeeType.EmployeeId
group by Department.Id, Department.Department, EmployeeType.EmployeeType
GO


现在 select * from VDepartmentEmployeeType
Id Department EmployeeType Cnt
----------- ---------- ------------ -----------
2 B 辞退 1
1 A 临时 1
1 A 正式 2
2 B 正式 2
有了这个结果,我们再通过行列转换,就可以实现要求的输出了
行列转换采用 case 分支语句来实现,如下:


select Id as ' 部门编号 ' , Department as ' 部门名称 ' ,
[ 正式 ] = Sum ( case when EmployeeType = ' 正式 ' then Cnt else 0 end ),
[ 临时 ] = Sum ( case when EmployeeType = ' 临时 ' then Cnt else 0 end ),
[ 辞退 ] = Sum ( case when EmployeeType = ' 辞退 ' then Cnt else 0 end ),
[ 合计 ] = Sum ( case when EmployeeType <> '' then Cnt else 0 end )
from VDepartmentEmployeeType
GROUP BY Id, Department


看一下结果
部门编号 部门名称 正式 临时 辞退 合计
----------- ---------- ----------- ----------- ----------- -----------
1 A 2 1 0 3
2 B 2 0 1 3
现在还有一个问题,如果员工类型不可以应编码怎么办?也就是说我们在写程序的时候并不知道有哪些员工类型。这确实是一个
比较棘手的问题,不过不是不能解决,我们可以通过拼接SQL的方式来解决这个问题。看下面代码


DECLARE
@s VARCHAR ( max )
SELECT @s = isnull ( @s + ' , ' , '' ) + ' [ ' + ltrim (EmployeeType) + ' ] = ' +
' Sum(case when EmployeeType = ''' +
EmployeeType + ''' then Cnt else 0 end) '
FROM ( SELECT DISTINCT EmployeeType FROM VDepartmentEmployeeType ) temp
EXEC ( ' select Id as 部门编号, Department as 部门名称, ' + @s +
' ,[合计]= Sum(case when EmployeeType <> '''' then Cnt else 0 end) ' +
' from VDepartmentEmployeeType GROUP BY Id, Department ' )


执行结果如下:
部门编号 部门名称 辞退 临时 正式 合计
----------- ---------- ----------- ----------- ----------- -----------
1 A 0 1 2 3
2 B 1 0 2 3
这个结果和前面硬编码的结果是一样的,但我们通过程序来获取了所有的员工类型,这样做的好处是如果我们新增了一个员工类型,比如“合同工”,我们不需要修改程序,就可以得到我们想要的输出。

如果你的数据库是SQLSERVER 2005 或以上,也可以采用SQLSERVER2005 通过的新功能 PIVOT


SELECT Id as ' 部门编号 ' , Department as ' 部门名称 ' , [ 正式 ] , [ 临时 ] , [ 辞退 ]
FROM
( SELECT Id,Department,EmployeeType,Cnt
FROM VDepartmentEmployeeType) p
PIVOT
( SUM (Cnt)
FOR EmployeeType IN ( [ 正式 ] , [ 临时 ] , [ 辞退 ] )
) AS unpvt


结果如下
部门编号 部门名称 正式 临时 辞退
----------- ---------- ----------- ----------- -----------
1 A 2 1 NULL
2 B 2 NULL 1
NULL 可以通过 ISNULL 函数来强制转换为0,这里我就不写出具体的SQL语句了。这个功能感觉还是不错,不过合计好像用这种方法不太好搞。不知道各位同行有没有什么好办法。

标签:SQL,行列转换
0
投稿

猜你喜欢

  • 朋友去一家游戏公司的机试题,被难住了

    2009-11-29 15:23:00
  • CSS 3入门

    2009-04-19 13:00:00
  • CSS Sprites (CSS图像拼合技术)教程工具

    2009-05-26 15:30:00
  • java EJB 加密与解密原理的一个例子

    2023-10-02 06:41:16
  • 基于layPage插件实现两种分页方式浅析

    2024-05-28 15:40:53
  • Python中五种实现字符串反转的方法

    2023-11-08 00:22:12
  • javascript数组去重方法总结(推荐)

    2024-04-17 10:31:22
  • Python中的Decorator装饰器的使用示例

    2023-07-02 10:37:44
  • MySQL安全策略(MySQL安全注意事项)

    2024-01-22 19:57:25
  • 基于python实现百度语音识别和图灵对话

    2022-08-23 23:44:54
  • python对excel文档去重及求和的实例

    2021-09-30 03:30:35
  • python爬取亚马逊书籍信息代码分享

    2021-03-25 09:38:14
  • 浅谈tensorflow 中tf.concat()的使用

    2023-07-21 20:24:08
  • 如何利用SQL Server 2005中的模板参数

    2009-01-23 15:02:00
  • python查看数据类型的方法

    2021-11-12 03:42:17
  • Python中的self用法详解

    2023-08-22 15:34:19
  • scrapy与selenium结合爬取数据(爬取动态网站)的示例代码

    2023-07-14 00:17:08
  • 详解如何模拟实现node中的Events模块(通俗易懂版)

    2024-05-05 09:20:55
  • 用SQL建立索引的方法步骤

    2024-01-21 20:43:43
  • Python OpenCV学习之图像形态学

    2022-01-16 21:48:29
  • asp之家 网络编程 m.aspxhome.com