SQl in PostgreSQL: No Repeat Values of a Table field

-1

I have a select that displays the names, date and values paid by clients. I have to display a list without repeating the names.

SELECT  
    c.name,
    p.created_at,
    p.amount_paid
FROM 
    payments as p, 
    clients as c
WHERE 
  p.created_at < CURRENT_DATE AND p.created_at > CURRENT_DATE -199

    
asked by anonymous 27.10.2017 / 16:31

1 answer

1

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;
    
27.10.2017 / 17:11