DB2 SQL之行合併(連線)

zchbaby2000發表於2017-01-16

建一個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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章