I have a table:
ComputerID, UserID, LoginDate
I need a select statement that returns:
ComputerID, UserID, LoginCount
For computers, all , but for each computer, only one user is displayed , which is most often registered on this computer . (If there is a tie, I think I would just like to randomly select one of the users ... so this would seem to indicate that I need TOP-1 somewhere.)
(This is in ms-access, therefore, cannot use provider-specific functions).
Solution (small correction of JBrooks answer)
select main.*
from (select ComputerID, UserID, count(1) as cnt
from ComputerLoginHistory
group by ComputerID, UserID) as main
inner join (select ComputerID, max(cnt) As maxCnt
from
(select ComputerID, UserID, count(1) as cnt
from ComputerLoginHistory
group by ComputerID, UserID) as Counts
group by ComputerID)
as maxes
on main.ComputerID = maxes.ComputerID
and main.cnt = maxes.maxCnt
, a > 1 loginCount , , , , Max (UserID)..... . , , , :
Select ComputerID, Max(xUserID) As UserID, MaxLoginDate
FROM
(
SELECT main.ComputerID, main.UserID as xUserID, main.MaxLoginDate
FROM [select ComputerID, UserID, Max(LoginDate) as MaxLoginDate
from ComputerLoginHistory
group by ComputerID, UserID]. AS main
INNER JOIN [select ComputerID, Max(MaxLoginDate) As MaxLogin
from
(select ComputerID, UserID, Max(LoginDate) as MaxLoginDate
from ComputerLoginHistory
group by ComputerID, UserID) as Counts
group by ComputerID]. AS maxes ON (main.MaxLoginDate = maxes.MaxLogin) AND (main.ComputerID = maxes.ComputerID)
)
GROUP BY ComputerID, MaxLoginDate
ORDER BY ComputerID