简体   繁体   English

将多行合并为一

[英]Combining multiple rows to one

I'm attempting to combine multiple names across rows to one column when the project_id matches. 当project_id匹配时,我试图将跨行的多个名称组合到一列。 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. 我从一个自我联接开始,但是无法使其按我想要的方式工作,我可以肯定有一个函数或CTE可以使此操作更加容易。 Asking for direction. 询问方向。 Working with db2. 使用db2。

Here is what I have so far which doesnt work and is producing a -104 error. 这是我到目前为止无法正常工作并产生-104错误的内容。

  (

        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: 数据示例: 在此处输入图片说明

This is the result that I need: 这是我需要的结果: 在此处输入图片说明

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. 在DB2 9.7和更高版本中可用的listagg()函数使用指定的定界符在组内通过串联聚合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM