简体   繁体   中英

inner join to get row with max column value

I have been trying to get the value of rows where the column value is max. but it's somehow not working. please help out a bit.

Let me explain the problem

table 1 name = agency columns agency_id, agency_name

table 2 name = client columns client_name, total_bill, agency_id

what i want to get is the client name, agency name and total_bill of the row for which total_bill value is maximum.

here's the query I was using

SELECT a.agency_name 
FROM agency a 
INNER JOIN (SELECT agency_id, client_name, MAX(total_bill) total_bill FROM client GROUP BY agency_id ) c 
ON a.agency_id = c.agency_id;

could you tell me where I am going wrong with this?

I am getting this error -

Expression #2 of SELECT list is not in GROUP BY clause and contains 
nonaggregated column 'agency-app.client.client_name' which is not 
functionally dependent on columns in GROUP BY clause; this is incompatible
with sql_mode=only_full_group_by

do let me know how to fix this.

If you want the overall top customer, you can just join, order and limit:

select 
    a.agency_name,
    c.client_name,
    c.total_bill
from agencies a
inner join clients c on c.agency_id = a.agency_id
order by c.total_bill desc
limit 1

Try this query:

SELECT client.name, agency.name, client.total_bill
FROM agency, client 
WHERE agency.agency_id=client.agency_id 
AND client.total_bill=(SELECT MAX(total_bill) FROM client)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM