次のレコードを持つ 2 つのテーブルがあります。
クライアント:
cid | cname | ccountry
-----------------------
1 | John | Australia
2 | Mark | USA
3 | Liz | England
注文:
oid | cid | oquantity
---------------------
1 | 1 | 100
2 | 1 | 100
3 | 2 | 50
4 | 2 | 150
5 | 3 | 50
6 | 3 | 100
注文数が最大のクライアント名を見つける必要があります。次のクエリを実行すると、正しい結果が得られました。
select cname, ccountry
from Clients
where cid in
(select cid
from Orders
group by cid
having sum(oquantity) = (select max(amount) from
(select sum(oquantity) amount
from Orders
group by cid)t1))
2 行が返されました
「ジョン」、「オーストラリア」
「マーク」、「アメリカ」
しかし、もっと簡単な方法でそれができるかどうかを知る必要があります。合計数量も返却する必要があると煩雑になってきました。