The tables receipt_status
and%% receipts
are not necessary in this query , they do not relate to any other table and none of its fields are recovered!
I could not see how to calculate "paid amounts paid by clients" if the client
table is not related in any way to the other tables!
I suggest you review the logic of your query , although it works, it certainly is not retrieving 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;