Atualmente tenho esta consulta:
select
sum(case when my_table.property_type = 'FLAT' then my_table.price else 0 end) as Flat_Current_Asking_Price,
sum(case when my_table.property_type_mapped = 'SEMIDETACHED' then my_table.price else 0 end) as Semidetached_Current_Asking_Price
from
my_table;
Então se my_table
tem os valores:
property_type | price
--------------+-------
FLAT | 5000
SEMIDETACHED | 9000
FLAT | 6000
a consulta retornará:
Flat_Current_Asking_Price | Semidetached_Current_Asking_Price
--------------------------+-----------------------------------
11000 | 9000
Como posso substituir o sum
para acumular os valores em arrays para obter?
Flat_Current_Asking_Price | Semidetached_Current_Asking_Price
--------------------------+-----------------------------------
{5000, 6000} | {9000}
Se sua versão do PostggreSQL for 9.4 ou posterior, use a
FILTER
cláusula:Se você quiser ter os arrays, use
array_agg
, em vez deSUM
:Se você quiser se livrar dos nulos, use um truque pequeno (array_to_string se livra deles):
dbfiddle aqui
termo aditivo
Por comentário de Abelisto, você também pode usar
array_remove
:dbfiddle aqui