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
答案 0 :(得分:8)
使用子查询做得最好:
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
答案 1 :(得分:2)
如果使用内部选择,它应该有效:
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
) AS T1
GROUP BY OnlineStatus
答案 2 :(得分:0)
您也可以将CASE WHEN
移至COUNT
伪代码:
SELECT
COUNT(CASE dbo.fnGetWebUserOnlineStatus(W.Id) WHEN 1 THEN 1 ELSE NULL END) as OnlineCount,
COUNT(CASE dbo.fnGetWebUserOnlineStatus(W.Id) WHEN 2 THEN 1 ELSE NULL END) as IngameCount ...
FROM dbo.WebUsers W
WHERE W.[Status]=1