Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microsoft SQL Count problem

Hey smarties. I'm having trouble with the following SQL statement. I know that I can't do a GROUP BY on the OnlineStatus column, and it makes sense because it's a function call, not an actual column in my table. How would I modify this so that I can get a count of how many users are online?

SELECT CASE dbo.fnGetWebUserOnlineStatus(W.Id) 
        WHEN 1 THEN 'Online' 
        WHEN 2 THEN 'Ingame' 
        ELSE 'Offline' 
       END AS OnlineStatus
FROM dbo.WebUsers W
WHERE W.[Status]=1
GROUP BY OnlineStatus
like image 796
Matt Avatar asked Dec 29 '22 20:12

Matt


1 Answers

That's best done using a subquery:

SELECT OnlineStatus, count(*)
FROM (
    SELECT  CASE dbo.fnGetWebUserOnlineStatus(W.Id) 
        WHEN 1 THEN 'Online' 
        WHEN 2 THEN 'Ingame' 
        ELSE 'Offline' 
         END AS OnlineStatus
    FROM dbo.WebUsers W
    WHERE W.[Status]=1
) sub
GROUP BY OnlineStatus
like image 197
Andomar Avatar answered Jan 12 '23 11:01

Andomar