Postgresql | Search with aggregation

Asked

Viewed 25 times

0

I’m beating myself up to bring in the sales number. I tried to make a Join of the tables, I studied what I needed and I used a Count, but it did not work well in the aggregation part.

Saída Esperada

Saída Atual

Code used:

SELECT
    to_char(
        pay.created_at + 
            (SELECT utc_offset 
            FROM pg_timezone_names 
            WHERE name = 'America/Sao_Paulo'), 
        'YYYY/MM/DD'
            ) as "Data Pagamento",
            
    ord.status as "Status",
    
    count(*) as "Compras"

FROM
    orders as ord
    
INNER JOIN 
    payments as pay on ord.id = pay.order_id

WHERE
    ord.status = 'approved'
    and
    pay.status = 'approved'
    
GROUP BY
    pay.created_at, ord.status

ORDER BY
    pay.created_at asc 
  • put in question the expected result

  • You make a GROUP BY pay.created_at, ord.status but its expected output does not contain the status field. I don’t know what kind of data from your field pay.created_at but you evaluated the use of AT TIME ZONE?

  • Thanks guys! It worked with the at time zone. Hugs!

No answers

Browser other questions tagged

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