2
我是PostgreSQL的新手。如何在PostgreSQL中使用動態字段來旋轉/交叉表
id | customer_id | form_id | field_id | field_name | form_submission_id | value |
---+-------------+---------+----------+------------+--------------------+--------------+
1 | 2 | 7 | c313 | Program | 1 | 2013 |
2 | 2 | 7 | c313 | Program | 2 | PIP |
3 | 2 | 7 | c313 | Program | 3 | CIP |
4 | 2 | 7 | c343 | Broker | 1 | broker test |
5 | 2 | 7 | c343 | Broker | 2 | broker test1 |
6 | 2 | 7 | c343 | Broker | 3 | broker test2 |
7 | 2 | 7 | c339 | Class | 1 | Class test |
8 | 2 | 7 | c339 | Class | 2 | Class test1 |
9 | 2 | 7 | c339 | Class | 3 | Class test2 |
我想作爲
customer_id form_id Program Broker Class form_submission_id
2 7 2013 broker test Class test 1
2 7 PIP broker test1 Class test1 1
2 7 CIP broker test2 Class test3 1
FIELD_NAME值將是動態的,不是固定值這樣的紀錄。
我試試這個,但得到這樣的錯誤「ERROR:迴歸和SQL元組的描述是不兼容的」
select * from crosstab (
'select Distinct customer_id ,form_id , field_name from form_submissions_reports '
)
as newtable (
customer_id integer,form_id integer,field_id1 varchar,field_id2 varchar,field_id3 varchar
);
但重要的是它的字段名稱是動態的。
我有谷歌,但沒有得到answerd –
是的,沒錯。相關的contrib是第一個結果,並且相關的StackOverflow dup是bext:http://stackoverflow.com/questions/3002499/postgresql-crosstab-query –
但它具有固定的狀態值。我沒有field_name的固定值 –