Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9078033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:31:13+00:00 2026-06-16T19:31:13+00:00

Possible Duplicate: Oracle Rows to Column Transformation My real problem is just like this

  • 0

Possible Duplicate:
Oracle Rows to Column Transformation

My real problem is just like this but has many tables related to each other. So, I just created a example, so assuming if this gets solved, my real problem gets solved.
Here are my tables :

tbl_products

    tp_id | tp_name 

     1      apple

     2      mango

     3      pineapple

tbl_sales

    ts_id | ts_location | ts_tp_id | ts_sales

       1        NY              2           5
       2        LN              2           10  
       3        QL              1           25          
       4        QL              3           20
       5        LN              3           35
       6        NY              3           50
       7        NY              1           100

If I have these two tables, tbl_products and tbl_sales, how to create a query that has a dynamic columns like this :

 sales_location | apple | mango | pineapple 
      NY           100       5        50

where apple, mango and pineapple should have the total number of sales according to location

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T19:31:15+00:00Added an answer on June 16, 2026 at 7:31 pm

    Since you are using Oracle10g, there is no PIVOT function so you will have to perform this type of transformation using an aggregate function with a CASE statement.

    If the values are known ahead of time, then you can hard code them in a static version:

    select s.ts_location,
      sum(case when p.tp_name = 'apple' then s.ts_sales else 0 end) Apple,
      sum(case when p.tp_name = 'mango' then s.ts_sales else 0 end) Mango,
      sum(case when p.tp_name = 'pineapple' then s.ts_sales else 0 end) Pineapple
    from tbl_sales s
    inner join tbl_products p
      on s.ts_tp_id = p.tp_id
    group by s.ts_location
    

    See SQL Fiddle with Demo

    But if you values are not known ahead of time, then you have to implement dynamic sql and in Oracle you will want to use a procedure for this:

    CREATE OR REPLACE procedure dynamic_pivot(p_cursor in out sys_refcursor)
    as
        sql_query varchar2(1000) := 'select s.ts_location ';
    
        begin
            for x in (select distinct tp_name from tbl_products order by 1)
            loop
                sql_query := sql_query ||
                    ' , sum(case when p.tp_name = '''||x.tp_name||''' then s.ts_sales end) as '||x.tp_name;
    
                    dbms_output.put_line(sql_query);
            end loop;
    
            sql_query := sql_query || ' from tbl_sales s 
                                                    inner join tbl_products p
                                                      on s.ts_tp_id = p.tp_id
                                                    group by s.ts_location';
            dbms_output.put_line(sql_query);
    
            open p_cursor for sql_query;
        end;
    /
    

    Then to return the results you can use (note: this is how I do it in Toad):

    variable x refcursor
    exec dynamic_pivot(:x)
    print x
    

    Both will return the result:

    | TS_LOCATION | APPLE | MANGO | PINEAPPLE |
    -------------------------------------------
    |          LN |     0 |    10 |        35 |
    |          QL |    25 |     0 |        20 |
    |          NY |   100 |     5 |        50 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: oracle PL/SQL: sort rows I run this query: Select a.product, sum( case
Possible Duplicate: Printing Table's structure/schema oracle has 'DESCRIBE' to get all the details of
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: Oracle SQL - How to Retrieve highest 5 values of a column
Possible Duplicate: Oracle SQL - How to Retrieve highest 5 values of a column
Possible Duplicate: Oracle SQL - How to Retrieve highest 5 values of a column
Possible Duplicate: Does Oracle fetch all the rows before evaluating rownum? If I run
Possible Duplicate: Oracle Search all tables all columns for string I'm looking for a
Possible Duplicate: How do I limit the number of rows returned by an Oracle
Possible Duplicate: Is there an Oracle SQL query that aggregates multiple rows into one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.