Error executing query in Postgresql

Asked

Viewed 849 times

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

1 answer

2


This error is telling you that functions like the SUM cannot be placed within the GROUP BY.

Try running 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

Browser other questions tagged

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