DB2 Distinct + xmlagg Request

I want the equivalent functionality of GROUP_CONCAT MySql in DB2.

I tried XML Aggrigate functon DB2 for murows coking.

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;

What gives me the result

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

But I want a separate / unique value BASIC_SKILL2, BASIC_SKILL1.

ID  |    SPECIALISATION |    BASIC_SKILL2   |   BASIC_SKILL1   |
----+-------------------+-------------------+------------------+
1   |    Java,C++       |  Development      |   Software       |
+3
source share
3 answers

Found a question by asking the same question myself. The solution I came up with is to use a common table expression with 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

In your case, it would be simpler and more understandable to use subqueries instead (for clarity, a correction of the XMLELEMENT template is output):

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

It may be easier, but this is what I came up with.

In addition, you can use features such as XMLQUERY and XSLTRANSFORM. Much easier and fewer errors than the manual way you do it.

+1

- , . .

, .

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
+1

How did you solve the problem with duplicates? Please help me using the XML function identifier Aggrigate | SPECIALIZATION | BASIC_SKILL2 | BASIC_SKILL1 | ---- + --------------------------------------------- ------------------------ + 1 | Java, C ++ | Development, Development | Software, Software |

0
source

All Articles