我有2个表SALESREP
和CUSTOMER
我需要找出哪个salesrep拥有大多数客户
我有以下代码:
select rep_lname, count(cust_num)
from customer inner join salesrep
on customer.REP_NUM = SALESREP.REP_NUM
group by rep_lname
这为我提供了每个salesrep拥有的客户数量的所有行,而我只需要一行拥有最多客户。
如何找到MAX num of customers的行?
答案 0 :(得分:1)
select rep_lname, count(cust_num)
from customer inner join salesrep
on customer.REP_NUM = SALESREP.REP_NUM
group by rep_lname order by count(cust_num) desc limit 1;
我确信使用having
有另一种方式,但我现在似乎无法弄明白。也许其他人会加入其中?
答案 1 :(得分:0)
SELECT TOP 1 WITH TIES rep_lname, COUNT(cust_num)
FROM customer inner join salesrep
ON customer.REP_NUM = SALESREP.REP_NUM
GROUP BY rep_lname
ORDER BY count(cust_num) DESC