我正在尝试获取具有最大客户端数的服务器的名称。我试图计算每个服务器的客户端数量,以及必须过滤的使用情况,但无法正常工作:
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);
答案 0 :(得分:2)
使用TOP
和ORDER 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;