0

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

4

1 回答 1

1

在外部选择中使用类似

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

listagg()函数在 DB2 9.7 及更高版本中可用,它使用指定的分隔符在组内通过串联进行聚合。

于 2013-08-17T12:39:23.197 回答