How to merge rows from a Virtual Table, based on a foreign key

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sesh

    #1

    How to merge rows from a Virtual Table, based on a foreign key

    The following is the sample result of a large SQL SELECT Statement, from a single table. I have used --- to show the X values under each column

    CODE------XX-------------YY-----------------ZZ
    AD------------X-----------------------------------
    AD-----------------------------------------------X
    AD---------------------------X--------------------
    CD------------X-----------------------------------
    CD-----------------------------------------------X
    CD---------------------------X--------------------

    I would like to further refine the above results by merging onto a single row for each CODE. Example below.

    Please note that each row for each code, will have only one value. So, using a CONCAT feature would be fine for me. I am at a loss to identify the parent SQL Statement to merge the data as below

    CODE--XX--YY--ZZ
    AD-------X---X---X
    CD-------X---X---X

    Request help from folks on this forum
  • amitpatel66
    Recognized Expert Top Contributor
    • Mar 2007
    • 2358

    #2
    Try this:

    [code=oracle]
    SQL> with t as (select 'AD' col1,'X' xx,NULL yy,NULL zz from dual
    2 union select 'AD',NULL,NULL, 'X' from dual
    3 union select 'AD',NULL,'X',N ULL from dual
    4 union select 'CD','X',NULL,N ULL from dual
    5 union select 'CD',NULL,'X',N ULL from dual)
    6 SELECT col1,MAX(xx),MA X(yy),MAX(zz) FROM t
    7 GROUP BY col1;

    CO M M M
    -- - - -
    AD X X X
    CD X X

    SQL>
    [/code]

    Comment

    Working...