Error when grouping per month with INNER Join using Postgres

Asked

Viewed 50 times

0

Good morning!

I’m trying to group my table by month but it’s not working..

I’m using this query:

SELECT "Boleto"."id", 
sum("valor_parcela") AS "volume_operado", 
sum("spread") AS "spread", 
sum("valor_pago") AS "liquidacoes", 
date_trunc('month', "Boleto"."criado_em") AS "criado_em", 
"boleto_taxa"."id" AS "boleto_taxa.id", 
"boleto_taxa"."valor" AS "boleto_taxa.valor" 
FROM "tb_boletos" AS "Boleto" 
INNER JOIN "tb_boletos_taxa" AS "boleto_taxa" 
ON "Boleto"."id" = "boleto_taxa"."boleto_id" 
AND "boleto_taxa"."taxa_id" = 1 
WHERE "Boleto"."operado" = true 
GROUP BY date_trunc('month', "Boleto"."criado_em"), "Boleto"."id", "boleto_taxa"."id"

That returns me this result:

id          | volume_operado | spread          | liquidacoes    | criado_em      | boleto_taxa_id |          
1           | 200.00         | 40.00           | 5.00           | 2020-05-28     |  1             |
2           | 300.00         | 50.00           | 6.00           | 2020-05-28     |  2             |

Being that I have 3 records, and I have 3 records (2 of the month of May) and he is not grouping these two records of the month of half because I had to put this data : "Boleto"." id", "boleto_taxa"." id" or postgres of the error that GROUP by when I do INNER JOIN

The question I believe it to be is: Group by when I don’t do the INNER Join it returns me separately by month but when I do, it error in GROUP BY in the table "bille_rate"." id".

Thank you for your attention

  • In Postgresql by definition select fields that are not an aggregation function must be in its GROUP BY clause. You may have to rethink your SQL expression construction.

  • @Matheus Lopes: The fields valor_parcela, spread and valor_pago belong to which tables? Explain the field taxa_id, it is part of the table tb_boletos_taxa? It is a foreign key ?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.