真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

DB2SQL之行合并(連接)

建一個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

現(xiàn)在想寫一個SQL,把一個部門的員工給做統(tǒng)計,每個部門一行
數(shù)據(jù)變成下面的樣子
 DEPT        NAMES
 ----------- ----------------
 Development John
 ID          Mark
 L3          Jack,Lee
 Quality     Lily
 Solutions   Berni,Miso,Serge

實現(xiàn)的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是把標量轉(zhuǎn)成XML的格式
select Dept,XMLELEMENT(NAME a, NAME) from Employee;
 DEPT        2
 ----------- ------------
 L3          Jack
 Quality     Lily
 ID          Mark
 L3          Lee
 Solutions   Serge
 Development John
 Solutions   Miso
 Solutions   Berni

XMLAGG把多個XML進行聚合,這里要給出分組的列(Dept),并且每個組里,以NAME進行排序
select Dept,XMLAGG(XMLELEMENT(NAME a, NAME) ORDER BY NAME) from Employee GROUP BY Dept;
 DEPT        2
 ----------- -----------------------------------
 Development John
 ID          Mark
 L3          Jack Lee
 Quality     Lily
 Solutions   Berni Miso Serge

XMLSERIALIZE()的作用是把XML轉(zhuǎn)換成為一個String類型
select Dept,XMLSERIALIZE(CONTENT XMLAGG(XMLELEMENT(NAME a, NAME) 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

到了這個地方就很簡單了,把 干掉,把 轉(zhuǎn)化成,即可

后來,出現(xiàn)了XMLGROUP,使用起來也比較方便 SELECT Dept
    ,XMLGROUP(',' || NAME AS a ORDER BY NAME)
FROM Employee
GROUP BY Dept

 DEPT        2
 ----------- ----------------------------------------------------------------------------------------
 Development ,John
 ID          ,Mark
 L3          ,Jack ,Lee
 Quality     ,Lily
 Solutions   ,Berni ,Miso ,Serge

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;

專業(yè)從事做網(wǎng)站、成都做網(wǎng)站,高端網(wǎng)站制作設(shè)計,小程序設(shè)計,網(wǎng)站推廣的成都做網(wǎng)站的公司。優(yōu)秀技術(shù)團隊竭力真誠服務(wù),采用成都h5網(wǎng)站建設(shè)+CSS3前端渲染技術(shù),響應(yīng)式網(wǎng)站設(shè)計,讓網(wǎng)站在手機、平板、PC、微信下都能呈現(xiàn)。建站過程建立專項小組,與您實時在線互動,隨時提供解決方案,暢聊想法和感受。


分享題目:DB2SQL之行合并(連接)
新聞來源:http://weahome.cn/article/ighscc.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部