我有一张prefs
桌子,下面是相关的列:
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
我想制作这样的东西:
section | pager_number | pager_carrier
---------+----------------+---------------
1 | 2125551234 |
2 | | @att.com
3 | | @something.com
所以我使用了交叉表,按照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);
所有值都进入pager_number
,并pager_carrier
留空:
section | pager_number | pager_carrier
---------+----------------+---------------
1 | 2125551234 |
2 | @att.com |
3 | @something.com |
谁能看到发生了什么?