mysql - multiple row fetched with max number sql -


i have 2 tables following records:

clients:

cid | cname | ccountry -----------------------   1 | john  | australia   2 | mark  | usa   3 | liz   | england 

orders:

oid | cid | oquantity ---------------------   1 |   1 |       100   2 |   1 |       100   3 |   2 |        50   4 |   2 |       150   5 |   3 |        50   6 |   3 |       100 

i need find out client name(s) has maximum quantity of orders. run following query , got correct result.

select cname, ccountry   clients  cid in          (select cid             orders         group cid           having sum(oquantity) = (select max(amount)                                        (select sum(oquantity) amount                                           orders                                         group cid)t1)) 

2 row(s) returned

'john', 'australia'

'mark', 'usa'

but need know, whether can done more simple way. has become complicated once total quantity required returned.

i have reduced 2 subqueries.

select   clients.cid,   cname,   ccountry orders,   clients orders.cid = clients.cid group clients.cid having sum(orders.oquantity) = (select                                   sum(oquantity) amount                                 orders                                 group cid                                 order amount desc                                 limit 1 ) 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -