个人运动---- 30天-----在线用户
RacingVehicle
个人运动---- 30天-----总用户
SELECT RoomId, Name,
ISNULL((SELECT COUNT(ChatUserLogId)
From ChatUserLog
WHERE RoomId = Room.RoomId
AND LoggedInTime >= DATEADD(DAY,-30,GETDATE())), 0) TotalUsers,
ISNULL((SELECT COUNT(LoggedInUserId)
From LoggedInUser
WHERE RoomID = Room.RoomID), 0) UserOnline
From Room
Where Sports = @Sports
ORDER BY UserOnline DESC
我需要对查询进行哪些更改才能获得所有运动项目的结果以及每项运动中前三项运动的结果?
表格详情
SELECT RoomId, Name,
ISNULL((SELECT COUNT(ChatUserLogId)
From ChatUserLog
WHERE RoomId = Room.RoomId
AND LoggedInTime >= DATEADD(DAY,-30,GETDATE())), 0) TotalUsers,
ISNULL((SELECT COUNT(LoggedInUserId)
From LoggedInUser
WHERE RoomID = Room.RoomID), 0) UserOnline
From Room
Where Sports = @Sports
ORDER BY TotalUsers DESC
答案 0 :(得分:0)
虽然缺少一些信息,例如每个查询的样本结果和您想要实现的结果样本,但我只能提供一些基本的想法。
当你删除查询中的Where Sports = @Sports
部分时,结果将不仅限于特定的运动,因此这可能符合您对“获得所有运动的结果”部分的需求。
“每项运动中独自前三名”这一部分对我来说并不十分清楚。您希望将值限制为哪个值?在您的查询结尾添加LIMIT 3
可能就足够了,但据我所知,我不清楚您希望得到什么结果。
如果这不是您想要实现的目标,您可以添加有关表格的更多信息以及查询的样本结果。也许你可以举例说明结果应该是什么样的?
答案 1 :(得分:0)
尝试以下查询,以获取在线用户的所有体育项目的前三名结果
每项特定运动---- 30天-----在线用户
select top 3 RoomId, Name,TotalUsers,UserOnline from
(SELECT RoomId, Name,ROW_NUMBER() over(partition by sports order by sports) rn,sports,
ISNULL((SELECT COUNT(ChatUserLogId)
From ChatUserLog
WHERE RoomId = Room.RoomId
AND LoggedInTime >= DATEADD(DAY,-30,GETDATE())), 0) TotalUsers,
ISNULL((SELECT COUNT(LoggedInUserId)
From LoggedInUser
WHERE RoomID = Room.RoomID), 0) UserOnline
From Room
)s where rn<=3 and sports=@sports
ORDER BY UserOnline DESC
如果您想要来自总用户,请尝试以下查询
每项特定运动---- 30天-----总用户数
select top 3 RoomId, Name,TotalUsers,UserOnline from
(SELECT RoomId, Name,ROW_NUMBER() over(partition by sports order by sports) rn,sports,
ISNULL((SELECT COUNT(ChatUserLogId)
From ChatUserLog
WHERE RoomId = Room.RoomId
AND LoggedInTime >= DATEADD(DAY,-30,GETDATE())), 0) TotalUsers,
ISNULL((SELECT COUNT(LoggedInUserId)
From LoggedInUser
WHERE RoomID = Room.RoomID), 0) UserOnline
From Room
)s where rn<=3 and sports=@sports
ORDER BY TotalUsers DESC
为了获得所有运动的结果,请尝试以下查询
个人运动---- 30天-----在线用户
SELECT RoomId, Name,
ISNULL((SELECT COUNT(ChatUserLogId)
From ChatUserLog
WHERE RoomId = Room.RoomId
AND LoggedInTime >= DATEADD(DAY,-30,GETDATE())), 0) TotalUsers,
ISNULL((SELECT COUNT(LoggedInUserId)
From LoggedInUser
WHERE RoomID = Room.RoomID), 0) UserOnline
From Room
ORDER BY UserOnline DESC
对于总用户 个人运动---- 30天-----总用户数
SELECT RoomId, Name,
ISNULL((SELECT COUNT(ChatUserLogId)
From ChatUserLog
WHERE RoomId = Room.RoomId
AND LoggedInTime >= DATEADD(DAY,-30,GETDATE())), 0) TotalUsers,
ISNULL((SELECT COUNT(LoggedInUserId)
From LoggedInUser
WHERE RoomID = Room.RoomID), 0) UserOnline
From Room
ORDER BY TotalUsers DESC