I want equivalent to GROUP_CONCAT functionality of MySql in DB2.
I have tried XML Aggrigate functon of DB2 for cocating 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;
Which gives me result
ID | SPECIALISATION | BASIC_SKILL2 | BASIC_SKILL1 |
----+---------------------------------------------------------------------+
1 | Java,C++ | Development,Development | Software,Software |
But I want distinct/Unique Value of BASIC_SKILL2,BASIC_SKILL1.
ID | SPECIALISATION | BASIC_SKILL2 | BASIC_SKILL1 |
----+-------------------+-------------------+------------------+
1 | Java,C++ | Development | Software |
The select distinct won't work in case of tables with no duplicates, because of multiple joins giving all the combinations of the values of every join. That leads to duplicates in the aggregate function.
I've found that pushing the group bys and the aggregate functions to subqueries in the from part gives the best results.
Came across your question after asking the same one myself. The solution I came up with is to use a common table expression with DISTINCT.
In your case, it would be easier and clearer to use subselects instead (XMLELEMENT boilerplate elided for clarity):
There may well be an easier way, but this is what I came up with.
Also, you might want to take advantage of functions like XMLQUERY and XSLTRANSFORM. Much simpler and less error-prone than the manual way you're doing it.