问题
I've a table containing information as follows:
idnumber applic_id cctype ccnumber
--------- ------------ -------- ----------
1 23 1 223445
2 23 2 345567
I need a query that an make this:
idnumber applic_id cctype ccnumber idnumber applic_id cctype ccnumber
--------- ------------ -------- ---------- --------- ------------ -------- ----------
1 23 1 223445 2 23 2 345567
Is anyone have a clue? I'm using PostgreSQL 8.3.
回答1:
You can use CASE statements for simple queries.
Or use the crosstab() function of the tablefunc module for more complex cases and better performance.
You can find examples for both cases under this related question:
PostgreSQL Crosstab Query
回答2:
This is known as a PIVOT.
You can do it either with the PIVOT keyword which does not exist in PostgreSQL, or using the poor-man's pivot like this:
Poor Man's Pivot:
- Need to pivot or crosstab a table but not in the conventional way. please
- Poor Man's SQL Pivot. List Questions as Columns and Answers per User in one row
来源:https://stackoverflow.com/questions/13744386/convert-multiple-rows-into-one-row-with-multiple-columns