Convert multiple rows into one row with multiple columns

只愿长相守 提交于 2019-12-23 02:06:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!