質問

I'm attempting to combine multiple names across rows to one column when the project_id matches. I started with a self join but could not get it to work the way I wanted it to and I'm pretty certain there is a function or cte that can do this a lot easier. Asking for direction. Working with db2.

Here is what I have so far which doesnt work and is producing a -104 error.

  (

        SELECT 
                        DP.D_P_ID, DP.project_name,
                        DU2.NAME_LAST CONCAT ', ' CONCAT DU2.NAME_FIRST AS 
                        FROM Fact_table as FAT 
                        INNER JOIN D_P DP ON FAT.D_P_ID = DP.D_P_ID
                        INNER JOIN B_U_P BUP on DP.D_P = BUP.D_P_ID
                        INNER JOIN D_U DU2 ON BUP.D_U_ID = DU2.D_U_ID
                        INNER JOIN D_Date DD ON FAT.START_DATE_ID = DD.DATE_KEY
                        INNER JOIN D_A DA ON FAT.D_A_ID = DA.D_A_ID
                        WHERE  ((    (DD.DATE_VALUE >= '2013-01-01')
                                OR (DD.DATE_VALUE < '2014-01-01')
                                OR (DD.DATE_VALUE <= '2013-01-01')))
                                AND DA.M_NAME = 'Mandy'
                                AND BUP.USER_FLAG = 'Y'

                        GROUP BY  DP.D_P_ID, DP.project_name, DU2.NAME_LAST CONCAT ', ' CONCAT DU2.NAME_FIRST
                        ORDER BY DP.project_name 
             )  PI1      

                 join 

                        (
                                SELECT 
                        DP.D_P_ID, DP.project_name,
                        DU2.NAME_LAST CONCAT ', ' CONCAT DU2.NAME_FIRST AS 
                        FROM Fact_table as FAT 
                        INNER JOIN D_P DP ON FAT.D_P_ID = DP.D_P_ID
                        INNER JOIN B_U_P BUP on DP.D_P = BUP.D_P_ID
                        INNER JOIN D_U DU2 ON BUP.D_U_ID = DU2.D_U_ID
                        INNER JOIN D_Date DD ON FAT.START_DATE_ID = DD.DATE_KEY
                        INNER JOIN D_A DA ON FAT.D_A_ID = DA.D_A_ID
                        WHERE  ((    (DD.DATE_VALUE >= '2013-01-01')
                                OR (DD.DATE_VALUE < '2014-01-01')
                                OR (DD.DATE_VALUE <= '2013-01-01')))
                                AND DA.M_NAME = 'Mandy'
                                AND BUP.USER_FLAG = 'Y'

                        GROUP BY  DP.D_P_ID, DP.project_name, DU2.NAME_LAST CONCAT ', ' CONCAT DU2.NAME_FIRST
                        ORDER BY DP.project_name 
                        ) PI2 on PI1.d_p_id = PI2.d_p_id 

Data example: enter image description here

This is the result that I need: enter image description here

役に立ちましたか?

解決

In the outer select use something like

select d_p_id, project_name, listagg(admin, ',')
from (...)
group by d_p_id, project_name

The listagg() function, available in DB2 9.7 and later, aggregates by concatenation within a group, using the specified delimiter.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top