get only one value for customer_ID where multiple rows for where

199 Views Asked by At

Hi all this is my query - if I run this I get multiple customer ids but I just want to select one customer id

SELECT payments.customer_id,
       payments.reason,
       payments.date
FROM payments  
WHERE reason like '%error%' 
ORDER BY payments.date DESC

This is the error message I get if i try to use a group by

Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'reason' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (errno 1055) (sqlstate 42000)

2

There are 2 best solutions below

0
On BEST ANSWER

Do you need below -

SELECT p.customer_id,
       p.reason,
       MAX(p.date) max_date
FROM payments p
WHERE p.reason like '%error%' 
GROUP BY p.customer_id,
         p.reason
ORDER BY max_date DESC
1
On
SELECT DISTINCT payments.customer_id, 
                payments.reason,
                payments.date FROM payments
WHERE reason like '%error%' 
ORDER BY payments.date DESC

DISTINCT will solve this for you