在子查询上使用“具有”

时间:2018-06-27 02:32:41

标签: sql sql-server

我正在尝试获取具有最大客户端数的服务器的名称。我试图计算每个服务器的客户端数量,以及必须过滤的使用情况,但无法正常工作:

SELECT s.id FROM (
SELECT s.id,COUNT(*) AS Total FROM Server s
JOIN Client v ON s.id = c.idServer
GROUP BY s.id)T 
HAVING Total = MAX(Total);

1 个答案:

答案 0 :(得分:2)

使用TOPORDER BY

SELECT TOP (1) WITH TIES s.id, COUNT(*) AS Total
FROM Server s JOIN
     Client v
     ON s.id = c.idServer
GROUP BY s.id
ORDER BY Total DESC;