PostgreSQL
 sql >> Base de Dados >  >> RDS >> PostgreSQL

Consulta complexa do Postgres


Tudo o que você precisa fazer é executar uma consulta agregada:
select sum(t.export) as TotalExport,
sum(t.import) as TotalImport
FROM country c inner join Organization o on c.Country_Code = o.Country_Code
inner join Transaction t on o.organization_code = t.organization_code 

Agora, você pergunta:onde está a coluna Corredor? A resposta é:use a função string_agg:
select string_agg(DISTINCT c.country, '-' ORDER BY c.country) as Corridor,
sum(t.export) as TotalExport,
sum(t.import) as TotalImport
FROM country c inner join Organization o on c.Country_Code = o.Country_Code
inner join Transaction t on o.organization_code = t.organization_code