0

我有 2 张桌子SALESREPCUSTOMER 我需要找出哪个销售代表拥有最多的客户

我有以下代码:

select rep_lname, count(cust_num) 
from customer inner join salesrep 
on customer.REP_NUM = SALESREP.REP_NUM 
group by rep_lname

这为我提供了每个销售代表拥有的客户数量的所有行,而我只需要拥有最多客户的行。

如何找到具有 MAX num of customers 的行?

4

2 回答 2

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,但目前我似乎无法弄清楚。也许其他人会附和它?

于 2013-01-13T05:48:50.720 回答
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
于 2013-01-13T08:47:47.397 回答