The tables receipt_status
and receipts
are not necessary in that query, they do not relate to any other table and none of their fields are recovered!
I couldn’t see how to calculate the "paid amounts paid by customers" if the table client
does not relate in any way to the other tables!
I suggest you review the logic of your query, although it works, it is certainly not recovering the information correctly.
The proposed solution would be something like:
SELECT
c.name,
p.created_at,
SUM(p.amount_paid) AS total_amount_paid
FROM
payments AS p
JOIN
payment_status AS ps ON ( ps.id = p.payment_status_id )
CROSS JOIN
clients AS c
WHERE
p.created_at BETWEEN current_date - '199 days'::interval AND current_date
GROUP BY
c.name,
p.created_at;
if you will not repeat the names, you will normally have to group date and value. You can use the first or last date, average or sum of values. What you need ?
– Rovann Linhalis
the idea is to add up the payments of each client
– alexjosesilva
as the form payments made the previous day, do not need to group by date
– alexjosesilva
but if it has multiple dates, you need to put in an aggregation function, or group by date
– Rovann Linhalis
no need. date field is unique: previous day!
– alexjosesilva
the goal is not to repeat the name of the customer and sum all payments made by him on the previous day
– alexjosesilva
in this case, the date enters the cluster. see the answer
– Rovann Linhalis
What is the table for
receipt_status
? Like the tableclients
is relating to others ? And the purpose of the tablereceipts
?? You want the sum ofamount_paid
grouped by the date of creationcreated_at
?– Lacobus
edited the code. But already published the answer to my problem!
– alexjosesilva
How about posting the original structure of the tables ?
– Lacobus
I am in doubt about selecting all tables fields!!
– alexjosesilva
how can I select other table fields ??
– alexjosesilva