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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:34:18+00:00 2026-06-02T06:34:18+00:00

I have a table with the following layout: ID Label Value — —– —–

  • 0

I have a table with the following layout:

 ID   Label   Value
 --   -----   -----
 1    Lab1    Value1-1
 1    Lab2    Value1-2
 1    Lab3    Value1-3
 1    Lab4    Value1-4
 1    Lab5    Value1-5
 1    Lab6    Value1-6
 2    Lab1    Value2-1
 2    Lab2    Value2-2
 2    Lab3    Value2-3
 2    Lab4    Value2-4
 2    Lab5    Value2-5
 2    Lab6    Value2-6
 ...

I’d like to convert the table to be laid out as follows:

 ID   Lab1      Lab2       Lab3       Lab4       Lab5       Lab6
 1    Value1-1  Value1-2   Value1-3   Value1-4   Value1-5   Value1-6
 2    Value2-1  Value2-2   Value2-3   Value2-4   Value2-5   Value2-6
 ...

I’m using SQL in PostgreSQL. Is there an easy (and memory efficient) way to do this?
I’ve seen some posts that mention using pivots, but I’m not sure if that would work and the descriptions I saw appeared to be specific to Oracle.

  • 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-02T06:34:19+00:00Added an answer on June 2, 2026 at 6:34 am

    First install the extension tablefunc, if you haven’t already. Needs to be done once per database.

    CREATE EXTENSION tablefunc;
    

    You need PostgreSQL 9.1 for CREATE EXTENSION. In older versions you have to run the install script from the shell with a command like:

    psql -d dbname -f SHAREDIR/contrib/tablefunc.sql
    

    More info for Postgres 9.0 in the fine manual.

    Then you can use a query like this one:

    SELECT *
    FROM   crosstab (
        'SELECT id
               ,label
               ,value
         FROM   t
         ORDER  BY 1, 2',
    
        'SELECT DISTINCT label
         FROM   t
         ORDER  BY 1')
    AS tbl (
     id   int
    ,lab1 text
    ,lab2 text
    ,lab3 text
    ,lab4 text
    ,lab5 text
    ,lab6 text
    );
    

    Returns exactly what you asked for.
    You can also create a function for that. I added more information in this closely related answer.

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

Sidebar

Related Questions

I have the following table layout. Each line value will always be unique. There
I have a table layout with the following code <table cellspacing=0 class=photogalleryTable> <tbody> <tr>
I have an SQL server with the following layout Table ( id int title
I've reduced my problem (table layout algorithm) to the following problem: Imagine I have
Say I have a table with the following layout: Id Int PRIMARY KEY IDENTITY
I have the following simplified layout: jsfiddle: http://jsfiddle.net/rersW/ <table> <tr> <td style=border:1px solid black;height:300px;width:100px>A</td>
I have the following layout: <div style=float:right ...>Contents</div> <table>contents</table> I want that the div
Say you have a table layout like the following: couses : id (INT), courseName
I have table with following structure and test records: +--------+--------+---------+ | vid | number
I have table with following structure Whenever user click on checkbook and click on

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.