最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

DB2 SQL query to combine multiple child rows to show in single row - Stack Overflow

programmeradmin0浏览0评论

I'm trying to build a query. I have a parent table, which has unique row for each account. Child table will have multiple rows for every single row on the parent table. Child table and parent table by the DB2 generated row ID.

Parent table.

ROW_ID      DATE        ACCT
1234563   2024-01-31   333555
1234564   2024-01-31   333666
1234565   2024-01-31   333777

Child table

ROW_ID     CRD_DB_YN    AMT   CODE
1234563         D       100     A
1234563         D       200     C
1234563         C       300     D
1234564         D       100     X
1234565         D       250     A
1234565         C       300     C

I want output from my query as below.

ROW_ID   ACCT     CRD_DB_YN   AMT    CODE  CRD_DB_YN   AMT    CODE  CRD_DB_YN   AMT    CODE
1234563   333555        D      100      A      D        200      C      C      300       D
1234564   333666        D      100      X 
1234565   333777        D      250      A      C        300      C

I'm trying to build a query. I have a parent table, which has unique row for each account. Child table will have multiple rows for every single row on the parent table. Child table and parent table by the DB2 generated row ID.

Parent table.

ROW_ID      DATE        ACCT
1234563   2024-01-31   333555
1234564   2024-01-31   333666
1234565   2024-01-31   333777

Child table

ROW_ID     CRD_DB_YN    AMT   CODE
1234563         D       100     A
1234563         D       200     C
1234563         C       300     D
1234564         D       100     X
1234565         D       250     A
1234565         C       300     C

I want output from my query as below.

ROW_ID   ACCT     CRD_DB_YN   AMT    CODE  CRD_DB_YN   AMT    CODE  CRD_DB_YN   AMT    CODE
1234563   333555        D      100      A      D        200      C      C      300       D
1234564   333666        D      100      X 
1234565   333777        D      250      A      C        300      C
Share Improve this question edited Jan 31 at 16:33 Filburt 18.1k12 gold badges86 silver badges145 bronze badges asked Jan 31 at 16:25 PrabhuPrabhu 8110 bronze badges 1
  • 1 You want a "pivot". Do you know the number of maximum columns in advance? If not, that would be a dynamic pivot, that you can do with a stored procedure; or you can do it in your app instead of doing it in the database. – The Impaler Commented Jan 31 at 16:52
Add a comment  | 

1 Answer 1

Reset to default 2

EDIT : In case the child level is not fixed, you can generate the above query using dynamic sql => Fiddle

In case your child level is fixed, you can first rank the children based on row_id and then based on the ranks the columns CRD_DB_YN , AMT and Code are repeated for each rank.

WITH rank_child AS (
  SELECT
    p.ROW_ID AS P_ROW_ID,
    p.ACCT,
    c.ROW_ID AS C_ROW_ID,
    c.CRD_DB_YN,
    c.AMT,
    c.CODE,
    ROW_NUMBER() OVER (PARTITION BY c.ROW_ID ORDER BY c.ROW_ID) AS rn
  FROM
    parent p
  INNER JOIN
    child c
  ON
    p.ROW_ID = c.ROW_ID
)
SELECT
  rc.P_ROW_ID AS ROW_ID,
  rc.ACCT,
  MAX(CASE WHEN rc.rn = 1 THEN rc.CRD_DB_YN END) AS CRD_DB_YN1,
  MAX(CASE WHEN rc.rn = 1 THEN rc.AMT END) AS AMT1,
  MAX(CASE WHEN rc.rn = 1 THEN rc.CODE END) AS CODE1,
  MAX(CASE WHEN rc.rn = 2 THEN rc.CRD_DB_YN END) AS CRD_DB_YN2,
  MAX(CASE WHEN rc.rn = 2 THEN rc.AMT END) AS AMT2,
  MAX(CASE WHEN rc.rn = 2 THEN rc.CODE END) AS CODE2,
  MAX(CASE WHEN rc.rn = 3 THEN rc.CRD_DB_YN END) AS CRD_DB_YN3,
  MAX(CASE WHEN rc.rn = 3 THEN rc.AMT END) AS AMT3,
  MAX(CASE WHEN rc.rn = 3 THEN rc.CODE END) AS CODE3
FROM
  rank_child rc
GROUP BY
  rc.P_ROW_ID, rc.ACCT
ORDER BY
  rc.P_ROW_ID;

Output

Fiddle

ROW_ID ACCT CRD_DB_YN1 AMT1 CODE1 CRD_DB_YN2 AMT2 CODE2 CRD_DB_YN3 AMT3 CODE3
1234563 333555 D 100 A D 200 C C 300 D
1234564 333666 D 100 X null null null null null null
1234565 333777 D 250 A C 300 C null null null
发布评论

评论列表(0)

  1. 暂无评论