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 8651181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:59:58+00:00 2026-06-12T13:59:58+00:00

I wanted to read a table which has values which will be the column

  • 0

I wanted to read a table which has values which will be the column names of the sql query result.
For example, I have table1 as ..

id    col1     col2
----------------------
0      name    ax
0      name2   bx
0      name3   cx
1      name    dx
1      name2   ex
1      name3   fx                

If you see for id = 0, name has value of ax and name2 is bx and name3 is cx.
Instead of this being rows it would be easier to show columns as id, name, name2, name3.
Now I want the result of the query to look like this:

id   name    name2     name3
0    ax      bx         cx
1    dx      ex         fx

Can someone help me in achieving this?

  • 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-12T13:59:59+00:00Added an answer on June 12, 2026 at 1:59 pm

    This is done with a pivot table. Grouping by id, you issue CASE statements for each value you want to capture in a column and use something like a MAX() aggregate to eliminate the nulls and collapse down to one row.

    SELECT
      id,
      /* if col1 matches the name string of this CASE, return col2, otherwise return NULL */
      /* Then, the outer MAX() aggregate will eliminate all NULLs and collapse it down to one row per id */
      MAX(CASE WHEN (col1 = 'name') THEN col2 ELSE NULL END) AS name,
      MAX(CASE WHEN (col1 = 'name2') THEN col2 ELSE NULL END) AS name2,
      MAX(CASE WHEN (col1 = 'name3') THEN col2 ELSE NULL END) AS name3
    FROM
      yourtable
    GROUP BY id
    ORDER BY id
    

    Here’s a working sample

    Note: This only works as is for a finite and known number of possible values for col1. If the number of possible values is unknown, you need to build the SQL statement dynamically in a loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to Python and wanted to read a file, which has even
I have a table called 'customer' which has too many rows & the decision
I read this question , which highlights a solution to conditionally insert values into
READ FIRST before answering! I have a RESTful service which wraps around the Entity
I wanted to make an associative array in Javascript in which I would have
OK so I have read a whole bunch of articles suggesting table-value functions and
I have an application which has a lot of functions which go through all
I started with a CSV file, which I read into a CSV::Table, 104 columns,
I had some data in an HTML table which wanted to be downloadable as
I have a couple of scenarios: Need to read the value of a column

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.