我已经尝试但是无法得到结果所以我在这里寻求帮助。我在计算机上有一个登录表。我想计算用户登录计算机的次数,然后获取最大值,以便最大用户被视为计算机的主要用户。
答案 0 :(得分:1)
根据您使用的dbms,您可以使用窗口函数ROW_NUMBER()来对每个comp的计数进行排名。然后拉出最高排名。
SELECT
Comp,
[User],
Cnt
FROM
(SELECT
Comp,
[User],
COUNT(1) AS Cnt,
ROW_NUMBER() OVER (PARTITION BY Comp ORDER BY Count(1) DESC) AS Rnk
FROM UserHist
GROUP BY Comp, [User]) BaseTable
WHERE Rnk = 1
答案 1 :(得分:1)
您可以RANK
计数,然后选择最大值。这使用CTE来获取具有计数和排名的数据。
WITH CTECOMP AS
(
SELECT COMP, [user], COUNT([USER]) [USERCOUNT],
RANK() OVER (PARTITION BY COMP ORDER BY COUNT([USER]) DESC ) [MYRANK]
FROM @TableData
GROUP BY comp, [user]
)
SELECT *
FROM CTECOMP
WHERE MYRANK = 1
答案 2 :(得分:0)
select Comp, [User], count([User]) as MaxCnt
from TableHere
group by Comp, [User]