我有一个用户和一个活动表。
用户拥有积分活动
用户 id,name
user_activities, id,user_id,points
现在我想获得最高分数低于100的用户列表:
select top 1[name], SUM(user_activities.points) as userpoints
from [users] inner join [user_activities] on [users].[id] = [user_activities].[user_id]
WHERE [userpoints] < 100
group by [name]
order by [userpoints] desc
不幸的是我收到一条错误消息:未知列用户点
答案 0 :(得分:1)
过滤聚合是通过HAVING:
完成的select top 1[name],
SUM(user_activities.points) as userpoints
from [users]
inner join [user_activities]
on [users].[id] = [user_activities].[user_id]
group by [name]
HAVING SUM(user_activities.points) < 100
order by SUM(user_activities.points) desc