7.某乎问答(内容行业)
SQL190 某乎问答11月份人均回答量
select
answer_date,
round(count(issue_id)/count(distinct author_id),2) as per_num
from
answer_tb
where month(answer_date)='11'
group by answer_date
SQL191 某乎问答高质量的回答中用户属于各级别的数量
select
(case when author_level in (5,6) then '5-6级'
when author_level in (3,4) then'3-4级'
else '1-2级' end) as level_cut,
count(issue_id) as num
from author_tb
join answer_tb using(author_id)
where char_len>=100
group by level_cut
order by num desc
SQL192 某户问答单日回答问题数大于等于3个的所有用户
select
answer_date,
author_id,
count(issue_id) as answer_cnt
from answer_tb
where month(answer_date)='11'
group by answer_date,author_id
having count(issue_id)>=3
order by answer_date,author_id
SQL193 某户问答回答过教育类问题的用户里有多少用户回答过职场类问题
- 首先,查找出回答过教育类问题的博主作为a表,查找出回答过职场类问题的博主作为b表。然后,对着两个表进行合并,求他们的交集,即为既回答过教育类问题,又回答过职场类问题的博主。最后,进行查询,即可得到数量。
select count(a.author_id) as num
from (
select author_id,
issue_type
from issue_tb
join answer_tb using(issue_id)
where issue_type='Education'
group by author_id
) as a
inner join
(
select author_id,
issue_type
from issue_tb
join answer_tb using(issue_id)
where issue_type='Career'
group by author_id
) as b
on a.author_id=b.author_id
- 也可以通过下面这种方法解答:
select count(t1.issue_id) as num
from answer_tb as t1
join issue_tb as t2 on t1.issue_id = t2.issue_id
where issue_type = 'Career'
and author_id in (
select author_id
from answer_tb as a
join issue_tb b on a.issue_id = b.issue_id
where issue_type = 'Education'
)
SQL194 某户问答最大连续回答问题天数大于等于3的用户及其对应等级
-
本题的难点在于计算出连续回答问题的天数。我们可以先查找出一个新表 表t ,对时间进行排序 1 2 3 …,然后用时间减去排序的序号,就可以得到dt。dt不变,就说明时间和连续的序号一样是同步变化的,即时间也是连续的;dt变化了,就说明,时间和连续的序号没有同步变化,时间不是连续的。
-
在表t 的基础上,我们对 author_id, author_level,以及dt 进行分组,然后用count求出时间的数量,就可以得到连续回答问题的最大天数(这里要用distinct进行去重,可参考上表,前两行完全一样,需要去重)。然后用having设置条件,使得输出的结果为最大连续回答问题的数量大于等于3的用户情况。
with t as(
select
answer_date,
a1.author_id,
author_level,
date_sub(answer_date,interval dense_rank() over(partition by a1.author_id order by answer_date) day ) as dt
from answer_tb as a1
left join author_tb as a2
on a1.author_id=a2.author_id
)
select
author_id,
author_level,
count(distinct answer_date) as days_cnt
from t
group by author_id,author_level,dt
having count(distinct answer_date)>=3
order by author_id