简体   繁体   中英

Select multiple rows into a single row one after the other in Postgres

I am using postgresql and have a table 'sometable' which looks like this

id | ref_id |  a_remarks   | a_date     |  b_remarks   |  b_date
1  | 32     |  'send xyz'  | 20/06/2014 |  'file send' |  22/06/2014 
2  | 32     |  'send abc'  | 25/06/2014 |  'file send' |  01/07/2014 

but while displaying it to user i need to display it this way

20/06/2014  Send xyz
22/06/2014  file send
25/06/2014  send abc
01/07/2014   file send

so i am unable to view data one after the other since it is in different columns. Can any one help me with this?? Thanks In Advance.

Create one query that selects a_remarks and a_date, union it with another query that selects b_remarks and b_date and then order by id. If it's important to strip out the id then create the unions in a subquery and just drop the id in the outer query. Like this...

SELECT t.date, t.remarks FROM (SELECT id, a_date AS date, a_remarks AS remarks FROM sometable UNION SELECT id, b_date AS date, b_remarks AS remarks ORDER BY id) t;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM