一個簡單的SQL 行列轉換語句

來源:互聯網
上載者:User

一個簡單的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語句了。這個功能感覺還是不錯,不過合計好像用這種方法不太好搞。不知道各位同行有沒有什麼好辦法。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.