DB2 SQL之行合併(連線)
建一個Employee表,有兩個列,一個是name,一個是所屬於的部門(dept)
CREATE TABLE Employee(name VARCHAR(15),dept VARCHAR(15));
insert into Employee values('Jack','L3');
insert into Employee values('Lily','Quality');
insert into Employee values('Mark','ID');
insert into Employee values('Lee','L3');
insert into Employee values('Serge','Solutions');
insert into Employee values('John','Development');
insert into Employee values('Miso','Solutions');
insert into Employee values('Berni','Solutions');
select * from Employee;
NAME DEPT
----- -----------
Jack L3
Lily Quality
Mark ID
Lee L3
Serge Solutions
John Development
Miso Solutions
Berni Solutions
現在想寫一個SQL,把一個部門的員工給做統計,每個部門一行
資料變成下面的樣子
DEPT NAMES
----------- ----------------
Development John
ID Mark
L3 Jack,Lee
Quality Lily
Solutions Berni,Miso,Serge
實現的SQL
SELECT Dept
,SUBSTR(Names, 1, LENGTH(names) - 1)
FROM (
SELECT Dept
,REPLACE(REPLACE(XMLSERIALIZE(CONTENT XMLAGG(XMLELEMENT(NAME a, NAME) ORDER BY NAME) AS VARCHAR(60)), '
', ',') AS Names
FROM Employee
GROUP BY Dept
) AS X;
解釋幾個DB2 XML方法的含義
XMLELEMENT是把標量轉成XML的格式
select Dept,XMLELEMENT(NAME a, NAME) from Employee;
DEPT 2
----------- ------------
L3
Quality
ID
L3
Solutions
Development
Solutions
Solutions
XMLAGG把多個XML進行聚合,這裡要給出分組的列(Dept),並且每個組裡,以NAME進行排序
select Dept,XMLAGG(XMLELEMENT(NAME a, NAME) ORDER BY NAME) from Employee GROUP BY Dept;
DEPT 2
----------- -----------------------------------
Development
ID
L3
Quality
Solutions
XMLSERIALIZE()的作用是把XML轉換成為一個String型別
select Dept,XMLSERIALIZE(CONTENT XMLAGG(XMLELEMENT(NAME a, NAME) ORDER BY NAME) AS VARCHAR(60)) from Employee GROUP BY Dept;
DEPT 2
----------- -----------------------------------
Development
ID
L3
Quality
Solutions
到了這個地方就很簡單了,把
轉化成,即可
後來,出現了XMLGROUP,使用起來也比較方便
SELECT Dept
,XMLGROUP(',' || NAME AS a ORDER BY NAME)
FROM Employee
GROUP BY Dept
DEPT 2
----------- ----------------------------------------------------------------------------------------
Development
ID
L3
Quality
Solutions
SELECT Dept
,XMLCAST(XMLGROUP(',' || NAME AS a ORDER BY NAME) AS VARCHAR(60))
FROM Employee
GROUP BY Dept
DEPT 2
----------- -----------------
Development ,John
ID ,Mark
L3 ,Jack,Lee
Quality ,Lily
Solutions ,Berni,Miso,Serge
SELECT Dept
,SUBSTR(XMLCAST(XMLGROUP(',' || NAME AS a ORDER BY NAME) AS VARCHAR(60)), 2) AS Names
FROM Employee
GROUP BY Dept
DEPT NAMES
----------- ----------------
Development John
ID Mark
L3 Jack,Lee
Quality Lily
Solutions Berni,Miso,Serge
到了DB2 9.7.4之後,這個問題得到了完美的解決
SELECT Dept,
LISTAGG(name, ',')
WITHIN GROUP (ORDER BY name)
FROM Employee
GROUP BY Dept;
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/725820/viewspace-2132501/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 兩表連線三:合併連線
- Oracle的表連線方法(一)排序合併連線Oracle排序
- Oracle表連線操作——Merge Sort Join(合併排序連線)Oracle排序
- Oracle連線Db2OracleDB2
- 【TUNE_ORACLE】列出走了錯誤的排序合併連線的SQL參考Oracle排序SQL
- 排序合併連線(sort merge join)的原理排序
- DB2 行列轉置之行轉列DB2
- KCSQL SERVER實現連線與合併查詢dinSQLServer
- 水煮oracle31----連線查詢&合併查詢Oracle
- 【SQL】表連線 --半連線SQL
- Oracle查詢轉換(三)外連線檢視合併Oracle
- oracle 到 db2 通用連線OracleDB2
- SQL Server連線SQL Server、SQL Server連線ORACLE 連結伺服器SQLServerOracle伺服器
- DB2行列轉置之行轉列DB2
- sql 內連線和外連線SQL
- SQL SERVER 自連線、外連線SQLServer
- Java 連線 DB2的工具類JavaDB2
- SQL Deverlop連線SQL ServerSQLdevServer
- 連線SQL ServerSQLServer
- SQL連線句法SQL
- SQL的四種連線:內連線 左外連線 右外連線 全連線SQL
- 線段樹合併 筆記筆記
- sql內連結,外連線SQL
- 深入淺出SQL之左連線、右連線和全連線SQL
- SQL中的左連線和右連線SQL
- 合併K個排序連結串列排序
- SQL Server 如何合併組內字串SQLServer字串
- python pandas Join SQL⻛格合併PythonSQL
- SQL連線查詢SQL
- SQL 三表連線SQL
- .NET連線SQL ServerSQLServer
- 利用trcsess合併以共享模式連線的session所產生的tracefile!模式Session
- 【SQL】Oracle的內連線、左外連線、右外連線及全外連線SQLOracle
- 微課sql最佳化(16)、表的連線方法(5)-關於Merge Join(排序合連線)SQL排序
- 深入理解SQL的四種連線-左外連線、右外連線、內連線、全連線SQL
- 連線DB2時出錯:encoding not supported??DB2Encoding
- Golang SQL連線池梳理GolangSQL
- SQL的連線型別SQL型別