Error executing query in PostgreSQL

1

After executing this query:

SELECT  
  p.name as nomerecebe,
  r.created_at as datarecebimento,
  r.original_amount as valorboleto,
  rs.name as statusr,
  SUM(r.original_amount) as total  
FROM 
  people as p,
  receipts as r,
  receipt_status as rs 
WHERE 
  p.id = r.person_id AND r.id = rs.id AND
  r.created_at < CURRENT_DATE AND r.created_at > CURRENT_DATE -199
GROUP BY 
    nomerecebe, datarecebimento, valorboleto, statusr, total

I get this error message:

ERROR:  aggregate functions are not allowed in GROUP BY LINE 6:  
SUM(r.original_amount) as total  
      ^

********** Error **********

ERROR: aggregate functions are not allowed in GROUP BY SQL state:
42803 Character: 129
    
asked by anonymous 27.10.2017 / 21:41

1 answer

2

This error is telling you that functions like SUM can not be placed inside GROUP BY .

Try to run the code like this:

SELECT  
  p.name as nomerecebe,
  r.created_at as datarecebimento,
  r.original_amount as valorboleto,
  rs.name as statusr,
  SUM(r.original_amount) as total  
FROM 
  people as p,
  receipts as r,
  receipt_status as rs 
WHERE 
  p.id = r.person_id AND r.id = rs.id AND
  r.created_at < CURRENT_DATE AND r.created_at > CURRENT_DATE -199
GROUP BY 
    nomerecebe, datarecebimento, valorboleto, statusr
    
27.10.2017 / 21:43