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

  • SEARCH
  • Home
  • 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 8299127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:02:13+00:00 2026-06-08T16:02:13+00:00

I need to turn columns into rows and get its average. For example I

  • 0

I need to turn columns into rows and get its average.

For example I have this table:

Name   Math    Science     Computer
----   ----    -------     --------
Ted    90       89          95
Zed    99       98          98
Fed    85       75          90

The output should be:

Subject      Average
-------      -------
Math         88
Science      87.33
Computer     94.33

How can I achieve it? Thank you for the help.

  • 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-08T16:02:15+00:00Added an answer on June 8, 2026 at 4:02 pm

    If you were on 11G you could use unpivot:

    SELECT subject, AVG(percentage) AS percentage
    FROM (
        SELECT * FROM tablea
        UNPIVOT (percentage FOR subject IN (math, science, computer))
    )
    GROUP BY subject
    ORDER BY subject;
    
    SUBJECT  PERCENTAGE
    -------- ----------
    COMPUTER      94.33
    MATH          91.33
    SCIENCE       87.33
    

    But since you are not, you can fake it. Adapting from this site:

    SELECT subject, AVG(percentage) AS percentage
    FROM (
        SELECT DECODE(unpivot_row, 1, 'Math',
                                   2, 'Science',
                                   3, 'Computer') AS subject,
               DECODE(unpivot_row, 1, math,
                                   2, science,
                                   3, computer) AS percentage
        FROM tablea
        CROSS JOIN (SELECT level AS unpivot_row FROM dual CONNECT BY level <= 3)
    )
    GROUP BY subject
    ORDER BY subject;
    
    SUBJECT  PERCENTAGE
    -------- ----------
    Computer      94.33
    Math          91.33
    Science       87.33
    

    In both cases, the inner select is transforming rows into columns; in 10g you just have to do it yourself. The SELECT ... CONNECT BY ... just generates a list of dummy values, and this has to have enough to cover the number of columns you are converting to rows (and if you really have 1000, you should really revisit the data model). The two decode statements use that generated number to match up a column name and value – run the inner select on its own to se what that looks like.

    Without resorting to dynamic SQL, you can’t get away from having to list the columns – only once with the real unpivot, but twice with the fake 10g version, and you have to make sure they match up properly, and that the row number generator is producing enough values. (Too many and you might get odd results, but as any extra values will be null here and you’re using avg, it doesn’t matter too much in this case; just as a sanity check you should probably make it match exactly anyway).


    Or another version, based on you always wanting all the columns except name, which means you only need to list the columns you do want once and it’s easier to match them up visually – just keep adding when clauses; and you don’t need the row count:

    SELECT subject, AVG(percentage) AS percentage
    FROM (
        SELECT column_name AS subject,
            CASE
                WHEN column_name = 'MATH' then math
                WHEN column_name = 'SCIENCE' then science
                WHEN column_name = 'COMPUTER' then computer
            END AS percentage
        FROM tablea
        CROSS JOIN (
            SELECT column_name
            FROM user_tab_columns
            WHERE table_name = 'TABLEA'
            AND column_name != 'NAME'
        )
    )
    GROUP BY subject
    ORDER BY subject;
    
    SUBJECT                        PERCENTAGE
    ------------------------------ ----------
    COMPUTER                            94.33
    MATH                                91.33
    SCIENCE                             87.33
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Pivon queries, love em. Turn rows into columns. I need to do a pivot
I have an projector file/Flash application that I need to turn into an interactive
I have a list of pointers that reference time objects that need a turn
I need to split a keyword string and turn it into a comma delimited
I have many timestamp columns that I need to use with mixed timezones. The
I have 2 million rows in a flat db4o table. A lot of the
I need to be able to turn a flat xml data sets into html
I need to turn a formatted integer into a regular integer: 000000000001 needs to
Short question: I need to turn a dynamic image pulled from a database into
I have a table, each row consists of 7 columns, and in the form

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.