我想编写一个存储过程作者的活动水平在博客上创建帖子时具有。
我想在call语句中输入用户名,如下call activity_level('Ugrob');
并得到以下结果
昵称 | level_activity | aupo_auth_idauthor_id | time_posting | [/tr>|
---|---|---|---|---|
Ugrob | 高 | 1 | 1 | 2003-02-05 13:28:57 |
有几个问题。首先,你是如何决定时间的?共有6个时间戳,作者"乌格罗布"有6个帖子。看看你的输出,它看起来像最大值。如果是这样,你需要在选择中指定它。
其次,您需要posts的计数来确定level_activity,而不是auth_id。所以你应该把它添加到你的案例中。
选择中的所有列都应该是groupby或aggregate函数的一部分。因此,您需要从选择中删除post_id。
您的选择如下所示。
select nickname, aupo_auth_id, author_id, max(time_posting), count(aupo_post_id),
case
when count(aupo_post_id) between 1 and 2 then 'low'
when count(aupo_post_id) between 3 and 5 then 'medium'
else 'high'
end as level_activity
from author_and_post join author_info
on author_id = aupo_auth_id join posts on post_id = aupo_post_id
where nickname = 'Ugrob'
group by nickname, aupo_auth_id, author_id
GROUP BY子句需要在SELECT子句中包含每个非聚合列。它缺少时间_端口和级别_活动