DBAsupport.com Forums - Powered by vBulletin
Results 1 to 3 of 3

Thread: How to convert row to columns

  1. #1
    Join Date
    Feb 2009
    Posts
    3

    How to convert row to columns

    Hi,

    Let me give you an example. I have a table with two column as bellow:

    id tracking_number
    2 1111
    2 2222
    2 3333
    2 4444
    2 5555

    I want the result that looks like this: (in single row with multiple columns)
    id trk_num1 trk_num2 trk_num3 trk_num4 trk_num5
    1 1111 2222 3333 4444 5555

    Please let me know how to write a query.
    Thanks in advance

  2. #2
    Join Date
    Mar 2007
    Location
    Ft. Lauderdale, FL
    Posts
    3,555
    Search this forum for pivot query.
    Pablo (Paul) Berzukov

    Author of Understanding Database Administration available at amazon and other bookstores.

    Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.

  3. #3
    Join Date
    Jan 2009
    Posts
    11
    hi
    try this one

    with data
    as
    (
    select 1 "ID",1111 "TNUM" from dual union all
    select 1, 2222 from dual union all
    select 2, 1111 from dual union all
    select 2, 2222 from dual union all
    select 2, 3333 from dual union all
    select 2, 4444 from dual union all
    select 2, 5555 from dual )
    select id,max(decode(rn,1 ,TNUM)) "trk_num1",
    max(decode(rn,2 , TNUM)) "trk_num2",
    max(decode(rn,3 , TNUM)) "trk_num3",
    max(decode(rn,4 , TNUM)) "trk_num4",
    max(decode(rn,5 ,TNUM)) "trk_num5"
    from (
    select id,TNUM,
    row_number() over (partition by id order by id) rn
    from data)
    group by id

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width