DB2 Distinct + xmlagg查询

时间:2011-06-13 05:25:00

标签: group-by db2 concatenation

我想要在DB2中等同于MySql的GROUP_CONCAT功能。

我已经尝试过DB2的XML Aggrigate功能,以寻找murows。

SELECT a.ID,
       substr(xmlserialize(xmlagg(xmltext( concat(',', SPECIALISATION)))as varchar( 1024 )),2),
       substr(xmlserialize(xmlagg(xmltext(concat(',,, BASIC_SKILL2)))as varchar( 1024 )),2),
       substr(xmlserialize(xmlagg(xmltext(concat(',', BASIC_SKILL1)))as varchar( 1024 )),2) 
FROM candidate_resume_data a,candidate_skills_info b,skill_special_master c,skill_master_basic2 d,skill_master_basic1 e      
WHERE e.SKILL_BASIC1_ID = d.SKILL_BASIC1_ID 
      AND b.ID = a.ID    
      AND d.SKILL_BASIC2_ID = c.SKILL_BASIC2_ID 
      AND b.CANDIDATE_SPECIALISATION_ID = c.SKILL_SPECIAL_ID 
GROUP BY a.ID;

这给了我结果

ID  |    SPECIALISATION |    BASIC_SKILL2           |   BASIC_SKILL1      |
----+---------------------------------------------------------------------+
1   |    Java,C++       |  Development,Development  |   Software,Software |

但是我想要BASIC_SKILL2的不同/唯一值,BASIC_SKILL1。

ID  |    SPECIALISATION |    BASIC_SKILL2   |   BASIC_SKILL1   |
----+-------------------+-------------------+------------------+
1   |    Java,C++       |  Development      |   Software       |

2 个答案:

答案 0 :(得分:1)

在自己问了同一个问题后遇到了你的问题。我想出的解决方案是使用带有DISTINCT的公用表表达式。

WITH q1 (id, specialization) AS
  (
    SELECT DISTINCT id, specialization
      FROM table_name
  )
SELECT q1.id,
    XMLELEMENT(
      NAME "Specializations",
      XMLAGG(
        XMLELEMENT(NAME "Specialization", q1.specialization)))
  FROM q1
  GROUP BY q1.id

在您的情况下,使用子选择会更容易和更清晰(为清晰起见,省略了XMLELEMENT样板):

SELECT t.id, XMLAGG(q1.specialization), XMLAGG(q2.basic_skill2),
    XMLAGG(q3.basic_skill1)
  FROM table_name t,
    (SELECT DISTINCT id, specialization FROM table_name) q1,
    (SELECT DISTINCT id, basic_skill2 FROM table_name) q2,
    (SELECT DISTINCT id, basic_skill1 FROM table_name) q3
  WHERE t.id = q1.id AND t.id = q2.id AND t.id = q3.id
  GROUP BY t.id

可能有一种更简单的方法,但这就是我想出来的。

此外,您可能希望利用XMLQUERY和XSLTRANSFORM等功能。比手动方式更简单,更不容易出错。

答案 1 :(得分:1)

如果表没有重复项,则select distinct将不起作用,因为多个连接提供了每个连接的值的所有组合。这导致聚合函数重复。

我发现将group by和聚合函数推送到from部分中的子查询可以得到最好的结果。

SELECT t.id, q1.values, q2.values, q3.values
FROM table_name t,
inner join (select t1.id, listagg(t1.value,',') as values
            from table_name1 t1 inner join table_name t on t.id=t1.id
            group by t1.id) q1 on t.id = q1.id
inner join (select t2.id, listagg(t2.value,',') as values
            from table_name2 t2 inner join table_name t on t.id=t2.id
            group by t2.id) q2 on t.id = q2.id
inner join (select t3.id, listagg(t3.value,',') as values
            from table_name3 t3 inner join table_name t on t.id=t3.id
            group by t3.id) q3 on t.id = q3.id