VERTICAL to HORIZONAL COLUMNS
Select messages from
# through # FAQ
[/[Print]\]

MVSFORUMS.com -> Database

#1: VERTICAL to HORIZONAL COLUMNS Author: NASCAR9Location: California PostPosted: Tue Dec 18, 2018 2:33 pm
    —
The data is in a vertical format with a key of FAMILY_ID that allows grouping a Family. The data looks something like this:

Code:

FAMILY_ID    RELATION_ID   FIRST_NAME   LAST_NAME
123456      0              HOBO      KELLY
123456      1              BOZO      CLOWN
123456      6              YUCKO      CLOWN   
123456      9              SIDESHOW   BOB
654321      0              ROY                 RODGERS
654321      2                      DALE                 EVANS
654321      3             CISCO      KID
654321      4             LUCAS      McCAIN   
654321      9             HOPALONG           CASSIDY


This query will flatten the data. I allowed for 10 FAMILY members in a FAMILY_ID. This is just an example.
Code:


 SELECT Z.FAMILY_ID,       
       MAX(DECODE(Z.RELATIONID_RANK, 01, Z.FIRST_NAME || ' ' || Z.LAST_NAME)) AS D1,
       MAX(DECODE(Z.RELATIONID_RANK, 02, Z.FIRST_NAME || ' ' || Z.LAST_NAME)) AS D2,
       MAX(DECODE(Z.RELATIONID_RANK, 03, Z.FIRST_NAME || ' ' || Z.LAST_NAME)) AS D3,
       MAX(DECODE(Z.RELATIONID_RANK, 04, Z.FIRST_NAME || ' ' || Z.LAST_NAME)) AS D4,
       MAX(DECODE(Z.RELATIONID_RANK, 05, Z.FIRST_NAME || ' ' || Z.LAST_NAME)) AS D5,
       MAX(DECODE(Z.RELATIONID_RANK, 06, Z.FIRST_NAME || ' ' || Z.LAST_NAME)) AS D6,
       MAX(DECODE(Z.RELATIONID_RANK, 07, Z.FIRST_NAME || ' ' || Z.LAST_NAME)) AS D7,
       MAX(DECODE(Z.RELATIONID_RANK, 08, Z.FIRST_NAME || ' ' || Z.LAST_NAME)) AS D8,
       MAX(DECODE(Z.RELATIONID_RANK, 09, Z.FIRST_NAME || ' ' || Z.LAST_NAME)) AS D9,
       MAX(DECODE(Z.RELATIONID_RANK, 10, Z.FIRST_NAME || ' ' || Z.LAST_NAME)) AS D10
    FROM
   (SELECT  FAMILY_ID, RELATION_ID, FIRST_NAME, LAST_NAME, 
           RANK() OVER ( PARTITION BY  FAMILY_ID   
                             ORDER BY  FAMILY_ID
                                    , RELATION_ID ) AS RELATIONID_RANK
   FROM YOUR.TABLE     
   WHERE FAMILY_ID   IN (123456, 654321)
   ORDER BY  FAMILY_ID, RELATION_ID ) Z
   GROUP BY Z.FAMILY_ID;


The result will be in columns. The reason I needed columns was for an Insert into another table

Code:


123456   HOBO KELLY    BOZO CLOWN   YUCKO CLOWN  SIDESHOW BOB
654321   ROY RODGERS   DALE EVANS   CISCO KID    LUCAS McCAIN   HOPALONG CASSIDY


I tried to line the data up so it would display correctly, but no joy.

Tried to edit for lineup, better but not pretty


Last edited by NASCAR9 on Wed Dec 19, 2018 10:48 am; edited 2 times in total

#2:  Author: kolusuLocation: San Jose PostPosted: Tue Dec 18, 2018 4:27 pm
    —
NASCAR9,

You can use the trick of using XMLAGG listed in here

https://www.mvsforums.com/helpboards/viewtopic.php?t=12879

NASCAR9 wrote:
The reason I needed columns was for an Insert into another table


Isn't it simple to do a INSERT INTO with a SELECT statement? Something like this
Code:

 INSERT INTO New_Table
     SELECT FAMILY_ID
           ,CHAR(RTRIM(IFNULL(FIRST_NAME, ' ')) ||         
            CHAR(' ')                           ||         
            RTRIM(IFNULL(LAST_NAME, ' '))) AS FULLNAME
      FROM Old_table
     WHERE FAMILY_ID IN (123456, 654321)
;

#3:  Author: NASCAR9Location: California PostPosted: Tue Dec 18, 2018 5:06 pm
    —
kolusu,

I have xml to do the concatenating of data and flatting.
xml returns the data in a single column(as far as I know). I'm familiar with the link, I participated in it Very Happy

The point of my post was to show how to take vertical data and be able to retrieve the data into individual columns. This query is only an example I'm sharing.


Last edited by NASCAR9 on Tue Jan 29, 2019 1:37 pm; edited 1 time in total

#4:  Author: kolusuLocation: San Jose PostPosted: Tue Dec 18, 2018 5:36 pm
    —
NASCAR9 wrote:
kolusu,
The point of my post was to show how to take horizonal data and be able to retrieve the data into individual columns. This query is only an example I'm sharing.


Nascar9,

I apologize for the oversight. Thanks for sharing the query.

#5:  Author: NASCAR9Location: California PostPosted: Tue Dec 18, 2018 7:08 pm
    —
kolusu,
No apology necessary. Your help is second to none.
I posted the query because I have never seen/used the Function DECODE before.
It solves a problem I'm faced with periodical. I'm hoping it's able to help someone else. There are many ways to skin a cat in SQL.



MVSFORUMS.com -> Database


output generated using printer-friendly topic mod. All times are GMT - 5 Hours

Page 1 of 1

Powered by phpBB © 2001, 2005 phpBB Group