POSTGRESQL中同一行中的两个结果

时间:2017-08-29 17:48:53

标签: sql postgresql

select * from Table1
where Condition 1
union 
select select * from Table1
where Condition 2

结果:

NAME    AMOUNT    TYPE   
ABC      --       Account
ABC      200        --

但我需要将结果放在一行,如

NAME     AMOUNT     TYPE
ABC       200       ACCOUNT

如何做PG?

2 个答案:

答案 0 :(得分:0)

根据您的样本数据,这可能有效

SELECT name,
       MAX(amount) amount,
       MAX(type) type
  FROM table1
 WHERE condition 1
    OR condition 2
 GROUP BY name

答案 1 :(得分:0)

我通过以下方式实现了类似的目标:

select TABLE_1.DESCRIPTION_COLUMN,

(array(select TABLE_2.INFO_COLUMN from TABLE_2 where TABLE_1.ID = TABLE_2.ID_TABLE_1))[1],
(array(select TABLE_2.INFO_COLUMN from TABLE_2 where TABLE_1.ID = TABLE_2.ID_TABLE_1))[2]

from TABLE_1


WHERE TABLE_1 = 1

来源/帮助:

https://stackoverflow.com/a/6402163/1856745