我正在尝试在project_id匹配时将多个名称跨行组合到一列。我开始自我加入,但无法按照我想要的方式工作,我很确定有一个函数或cte可以做到这一点更容易。要求方向。使用db2。
到目前为止,这是我无法正常工作并产生-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
数据示例:
这是我需要的结果:
答案 0 :(得分:1)
在外部选择使用像
这样的东西select d_p_id, project_name, listagg(admin, ',')
from (...)
group by d_p_id, project_name
DB2 9.7及更高版本中提供的listagg()
函数使用指定的分隔符通过组内的串联进行聚合。