简体   繁体   English

在Postgres中将多行选择为一行

[英]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 我正在使用postgresql并有一个看起来像这样的表“ sometable”

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. 创建一个选择a_remarks和a_date的查询,将其与另一个选择b_remarks和b_date然后按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. 如果删除ID很重要,则在子查询中创建并集,然后将ID放在外部查询中即可。 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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