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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:05:21+00:00 2026-06-08T20:05:21+00:00

I have a prefs table, and here are the relevant columns: mydb=> SELECT pref_id,

  • 0

I have a prefs table, and here are the relevant columns:

mydb=> SELECT pref_id, pref_name, pref_value FROM prefs;
 pref_id |  pref_name   |   pref_value
---------+--------------+----------------
       1 | PagerNumber  | 2125551234
       2 | PagerCarrier | @att.com
       3 | PagerCarrier | @something.com

I want to produce something like this:

 section |  pager_number  | pager_carrier
---------+----------------+---------------
       1 | 2125551234     |
       2 |                | @att.com
       3 |                | @something.com

So I used crosstab, following this example on stackoverflow: PostgreSQL Crosstab Query

SELECT row_name AS section,
       category_1::text AS pager_number,
       category_2::text AS pager_carrier
FROM crosstab('select pref_id::bigint, pref_name::text, pref_value::text
    FROM prefs')
AS ct (row_name bigint, category_1 text, category_2 text);

All the values are going into pager_number, and pager_carrier is left empty:

 section |  pager_number  | pager_carrier
---------+----------------+---------------
       1 | 2125551234     |
       2 | @att.com       |
       3 | @something.com |

Can anyone see what’s going on?

  • 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-08T20:05:22+00:00Added an answer on June 8, 2026 at 8:05 pm

    Test case (preferred way to provide sample data):

    CREATE TEMP TABLE prefs (pref_id int, pref_name text, pref_value text);
    
    INSERT INTO prefs VALUES 
     (1, 'PagerNumber' , '2125551234')
    ,(2, 'PagerCarrier', '@att.com')
    ,(3, 'PagerCarrier', '@something.com');
    

    Query:

    SELECT *
    FROM   crosstab(
           'SELECT pref_id, pref_name, pref_value
            FROM   prefs
            ORDER  BY 1, 2',
    
           $$VALUES ('PagerNumber'::text), ('PagerCarrier')$$
           )
    AS x (section text, pager_number bigint, pager_carrier text);
    

    Returns exactly the result depicted in your question. If a PagerNumber can be something else than a valid bigint number, use text instead.

    The answer you are referring to in your question was outdated and never correct to begin with. I added a proper answer with explanation and links over there.

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

Sidebar

Related Questions

I have table with ajax paging, i can select any page and view data,
Here's my case: I have a table view showing contacts. Add button in the
Here is the scenario. I have 1 table that has all the contact details.
I have a table view with numerous cells. When I press one, a tick
I have a widget with a prefs activity. In that activity the user is
I have a table view with a search bar at the top of the
I have a table listing with a 'notes' field in each row. I'd like
Here's the setup: I have a tab bar controller with two tabs. There is
Here's the question : I have 2 tables , one containing the base data
I'm using storyboard to create a grouped UITableView . Within the table I have

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.