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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:27:42+00:00 2026-05-25T19:27:42+00:00

I am new to PostgreSQL. Suppose I have a table as under colorname Hexa

  • 0

I am new to PostgreSQL.

Suppose I have a table as under

colorname   Hexa    rgb rgbvalue
Violet  #8B00FF r   139
Violet  #8B00FF g   0
Violet  #8B00FF b   255
Indigo  #4B0082 r   75
Indigo  #4B0082 g   0
Indigo  #4B0082 b   130
Blue    #0000FF r   0
Blue    #0000FF g   0
Blue    #0000FF b   255

If I do a Pivot in SQL Server as

SELECT colorname,hexa,[r], [g], [b]
FROM
(SELECT colorname,hexa,rgb,rgbvalue
    FROM tblPivot) AS TableToBePivoted
PIVOT
(
sum(rgbvalue)
FOR rgb IN ([r], [g], [b])
) AS PivotedTable;

I get the output as

colorname   hexa    r   g   b
Blue    #0000FF 0   0   255
Indigo  #4B0082 75  0   130
Violet  #8B00FF 139 0   255

How to do the same using PostgreSQL?

My attempt is

SELECT *
FROM crosstab
(
    'SELECT 
        colorname
        ,hexa
        ,rgb
        ,rgbvalue
    FROM tblPivot'
)AS ct(colorname text, hexa text, rgb text, rgbvalue int);

But geting error:

ERROR:  function crosstab(unknown) does not exist
LINE 2: FROM crosstab
             ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
********** Error **********
ERROR: function crosstab(unknown) does not exist**

Is there any elegant way of doing so in PostgreSQL (any built in function…) What is the standard practice of doing so ?

  • 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-05-25T19:27:42+00:00Added an answer on May 25, 2026 at 7:27 pm

    This can be expressed as a JOIN:

    SELECT c.colorname, c.hexa, r.rgbvalue, g.rgbvalue, b.rgbvalue
    FROM (SELECT colorname, hexa
          FROM sometable
          GROUP BY colorname) c
    JOIN sometable r ON c.colorname = r.colorname AND r.rgb = 'r'
    JOIN sometable g ON c.colorname = g.colorname AND g.rgb = 'g'
    JOIN sometable b ON c.colorname = b.colorname AND b.rgb = 'b'
    ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to PostgreSQL. I have tables like: CREATE TABLE Person ( ID SERIAL
I'm new to PostgreSQL (I have been using MS SQL for many years) and
I have an existing PostgreSQL database that I'm using for a new Django site.
In PostgreSQL, I created a new table and assigned a new sequence to the
I am new to PostgreSQL. I have a very unusual requirement for a hybrid
I have created a new schema in my PostgreSQl database with psql: CREATE SCHEMA
I'm new to postgreSQL and I have a simple question: I'm trying to create
Suppose I have a table partitioned by month in postgres where the following are
I'm new to postgreSQL, so would really appreciate any pointers from the community. I
I'm new to PostgreSQL. I was wondering, how do I make a column a

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.