PostgreSQL crosstab/pivot problems

2020-07-24 03:45发布

问题:

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:

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.



回答2:

Instead of:

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);

try:

SELECT *
FROM crosstab('select pref_id::bigint, pref_name::text, pref_value::text
FROM prefs ORDER BY 1,2')
AS prefs (row_name bigint, carrier_1 text, carrier_2 text);

If you had:

    pref_id |  pref_name   |   pref_value
   ---------+--------------+----------------
   1 | PagerNumber  | 2125551234
   2 | PagerCarrier | @att.com
   3 | PagerCarrier | @something.com
   2 | PageNumber   | 2332323232
   3 | PagerCarrier | @somethingelse.com

You would have gotten:

    row_name |  carrier_1   |   carrier_2
   -----+--------------+----------------
   1 | 2125551234      |
   2 | @att.com        | 2332323232
   3 | @something.com  | @somethingelse.com

Postgress Crosstab Reference