首页 > 试题广场 >

某乎问答高质量的回答中用户属于各级别的数量

[编程题]某乎问答高质量的回答中用户属于各级别的数量
  • 热度指数:24861 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
现有某乎问答创作者信息表author_tb如下(其中author_id表示创作者编号、author_level表示创作者级别,共1-6六个级别、sex表示创作者性别):
author_id author_level sex
101 6 m
102 1 f
103 1 m
104 3 m
105 4 f
106 2 f
107 2 m
108 5 f
109 6 f
110 5 m
创作者回答情况表answer_tb如下(其中answer_date表示创作日期、author_id指创作者编号、issue_id指问题编号、char_len表示回答字数):
answer_date author_id issue_id char_len
2021-11-01 101 E001 150
2021-11-01
101 E002 200
2021-11-01
102 C003 50
2021-11-01
103 P001 35
2021-11-01
104 C003 120
2021-11-01
105 P001 125
2021-11-01
102 P002 105
2021-11-02
101 P001 201
2021-11-02
110 C002 200
2021-11-02
110 C001 225
2021-11-02
110 C002 220
2021-11-03
101 C002 180
2021-11-04
109 E003 130
2021-11-04
109 E001 123
2021-11-05
108 C001 160
2021-11-05
108 C002 120
2021-11-05
110 P001 180
2021-11-05
106 P002 45
2021-11-05
107 E003 56
回答字数大于等于100字的认为是高质量回答,请你统计某乎问答高质量的回答中用户属于1-2级、3-4级、5-6级的数量分别是多少,按数量降序排列,以上例子的输出结果如下:
level_cut num
5-6级 12
3-4级
2
1-2级
1

示例1

输入

drop table if exists author_tb;
CREATE TABLE author_tb(
author_id int(10) NOT NULL, 
author_level int(10) NOT NULL,
sex char(10) NOT NULL);
INSERT INTO author_tb VALUES(101 , 6, 'm');
INSERT INTO author_tb VALUES(102 , 1, 'f');
INSERT INTO author_tb VALUES(103 , 1, 'm');
INSERT INTO author_tb VALUES(104 , 3, 'm');
INSERT INTO author_tb VALUES(105 , 4, 'f');
INSERT INTO author_tb VALUES(106 , 2, 'f');
INSERT INTO author_tb VALUES(107 , 2, 'm');
INSERT INTO author_tb VALUES(108 , 5, 'f');
INSERT INTO author_tb VALUES(109 , 6, 'f');
INSERT INTO author_tb VALUES(110 , 5, 'm');

drop table if exists answer_tb;
CREATE TABLE answer_tb(
answer_date date NOT NULL, 
author_id int(10) NOT NULL,
issue_id char(10) NOT NULL,
char_len int(10) NOT NULL);
INSERT INTO answer_tb VALUES('2021-11-1', 101, 'E001' ,150);
INSERT INTO answer_tb VALUES('2021-11-1', 101, 'E002', 200);
INSERT INTO answer_tb VALUES('2021-11-1',102, 'C003' ,50);
INSERT INTO answer_tb VALUES('2021-11-1' ,103, 'P001', 35);
INSERT INTO answer_tb VALUES('2021-11-1', 104, 'C003', 120);
INSERT INTO answer_tb VALUES('2021-11-1' ,105, 'P001', 125);
INSERT INTO answer_tb VALUES('2021-11-1' , 102, 'P002', 105);
INSERT INTO answer_tb VALUES('2021-11-2',  101, 'P001' ,201);
INSERT INTO answer_tb VALUES('2021-11-2',  110, 'C002', 200);
INSERT INTO answer_tb VALUES('2021-11-2',  110, 'C001', 225);
INSERT INTO answer_tb VALUES('2021-11-2' , 110, 'C002', 220);
INSERT INTO answer_tb VALUES('2021-11-3', 101, 'C002', 180);
INSERT INTO answer_tb VALUES('2021-11-4' ,109, 'E003', 130);
INSERT INTO answer_tb VALUES('2021-11-4', 109, 'E001',123);
INSERT INTO answer_tb VALUES('2021-11-5', 108, 'C001',160);
INSERT INTO answer_tb VALUES('2021-11-5', 108, 'C002', 120);
INSERT INTO answer_tb VALUES('2021-11-5', 110, 'P001', 180);
INSERT INTO answer_tb VALUES('2021-11-5' , 106, 'P002' , 45);
INSERT INTO answer_tb VALUES('2021-11-5' , 107, 'E003', 56);

输出

5-6级|12
3-4级|2
1-2级|1
我来重新翻译一下这个有歧义的需求:

需求:回答字数大于等于100字的认为是高质量回答,请你统计某乎问答高质量的回答中用户属于1-2级、3-4级、5-6级的数量分别是多少

翻译:请你统计所有1-2级、3-4级、5-6级的用户所回答的高质量回答(回答字数>= 100)的数量,结果按用户类别进行展示,答题数量降序排列。

一句话,我们要统计的是答题数,不是用户数。


发表于 2021-12-07 09:15:13 回复(6)
本题的关键点在于case  when的使用,具体使用参照:
select (case when author_level>=5 then '5-6级'
       when author_level<=2 then '1-2级'
       else '3-4级' end)level_cut,
       count(answer_tb.author_id) num 
       from author_tb
       join answer_tb
       on answer_tb.author_id=author_tb.author_id
       where char_len>=100
       group by level_cut
       order by num desc


发表于 2021-12-03 14:37:22 回复(0)
select level_cut,count(level_cut) ct
from
(
    select 
    (case when author_level in (1,2) then '1-2级'
          when author_level in (3,4) then '3-4级'
          when author_level in (5,6) then '5-6级' end) level_cut
    from answer_tb an
    left join author_tb au
    on an.author_id = au.author_id
    where char_len >= 100
) t
group by level_cut
order by ct desc

发表于 2021-12-20 22:03:49 回复(0)

【场景】:每个级别的用户数量

【分类】:分组查询、条件函数、case when

分析思路

难点:

1.统计用户数量不是用户数,一个不去重,一个去重

(1)统计某乎问答高质量的回答中用户属于1-2级、3-4级、5-6级的数量分别是多少,按数量降序排列

  • [条件]:回答字数大于等于100字

  • [使用]:case when

最终结果

select 查询结果 [等级;用户数量]
from 从哪张表中查询数据 [创作者信息表;创作者回答情况表]
where 查询条件 [回答字数大于等于100字]
group by 分组条件 [等级]
order by 对查询结果排序 [用户数量];

求解代码

方法一

case when

select
    (case
        when author_level between 1 and 2 then '1-2级'
        when author_level between 3 and 4 then '3-4级'
        else '5-6级'
    end) as level_cut,
    count(author_id) as num
from author_tb
join answer_tb using(author_id)
where char_len >= 100
group by level_cut
order by num desc
发表于 2022-12-03 17:28:38 回复(0)
老铁们,请问分别求,然后用union连接,为什么结果不对嘞?
发表于 2022-05-20 20:26:49 回复(3)
select level_cut,count(issue_id) as num 
from 
(select *,(case when author_level in (1,2) then "1-2级"
          when author_level in (3,4) then "3-4级"
          when author_level in (5,6) then "5-6级"
          end) as level_cut
from author_tb) t,answer_tb t2 
where char_len>=100 and t.author_id=t2.author_id
group by level_cut
order by num desc 

发表于 2022-04-11 17:12:00 回复(0)
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(b.author_id) as num
from 
(
select
    author_id,author_level  
    from author_tb
)a 
join
(
select
    answer_date,author_id,issue_id,char_len
    from answer_tb
    where char_len>='100'
)b
on a.author_id=b.author_id
group by 1
order by num desc

发表于 2023-12-11 00:37:22 回复(0)
select
    a.level as level_cut,
    sum(b.hq_ans) as num
from
(select
    (case when author_level = 6 or author_level = 5 then '5-6级'
          when author_level = 4 or author_level = 3 then '3-4级'
          when author_level = 2 or author_level = 1 then '1-2级'
        else null end ) as level,
    author_id
from author_tb) a
join(select author_id, count(*) as hq_ans
      from answer_tb
      where char_len >= 100
      group by author_id) b on a.author_id = b.author_id
group by level_cut
order by num desc
发表于 2023-05-08 15:12:53 回复(0)
select 
'1-2级' as level_cut,
count(author_tb.author_id) as num
from answer_tb
join author_tb
using (author_id)
where author_tb.author_level in (1,2)
and answer_tb.char_len>=100

union all

select
'3-4级' as level_cut,
count(author_tb.author_id) as num
from answer_tb
join author_tb
using (author_id)
where author_tb.author_level in (3,4)
and answer_tb.char_len>=100

union all

select
'5-6级' as level_cut,
count(author_tb.author_id) as num
from answer_tb
join author_tb
using (author_id)
where author_tb.author_level in (5,6)
and answer_tb.char_len>=100

order by num desc
为什么这么写最后运行出来的‘1-2级’是0呢
发表于 2022-08-12 16:33:43 回复(3)
很奇怪。自测时通过了,提交时1-2级变成0了,为什么
select*
from(
    SELECT '1-2级' level_cut,count(author_level) num
    from answer_tb t1 left join author_tb t2 on t1.author_id=t2.author_id
    where char_len>=100 and author_level in(1,2)
    union 
    SELECT '3-4级' level_cut,count(author_level) num
    from answer_tb t1 left join author_tb t2 on t1.author_id=t2.author_id
    where char_len>=100 and author_level in(3,4)
    union
    SELECT '5-6级' level_cut,count(author_level) num
    from answer_tb t1 left join author_tb t2 on t1.author_id=t2.author_id
    where char_len>=100 and author_level in(5,6)) t3
order by  t3.num desc
发表于 2022-05-09 11:49:02 回复(4)
根据sql的执行顺序应该是先执行group by 的内容在执行select 的内容。为什么答案先执行 select 再执行 group by 
发表于 2022-03-21 13:43:40 回复(3)
这道题的答案应该给的是的回答数量,而不是用户数。
参考下图的运行结果截图:
5-6级的回答数量有12个,而用户数只有4个(101,108,109,110)


SELECT
	level_cut,
	COUNT(author_id) as num
FROM
(SELECT
	 t1.author_id
	,(case when author_level >=5 then "5-6级"
		    when author_level BETWEEN 3 and 4 then "3-4级"
	 else "1-2级" end) level_cut	
FROM
(SELECT
	  author_id
	 ,author_level 
 FROM author_tb
) t1
JOIN 
(SELECT
	*
FROM answer_tb
WHERE char_len>=100) t2
ON t1.author_id = t2.author_id) t3
GROUP BY level_cut
ORDER BY num desc




发表于 2022-03-10 18:12:55 回复(0)
# 高质量回答 >=100
select level_cut,
count(c.author_id) as num
from (
select 
b.author_id,
case when a.author_level in (1,2) then '1-2级'
when a.author_level in (3,4) then '3-4级'
when a.author_level in (5,6) then '5-6级'
end as level_cut 
from author_tb a
right join answer_tb b
on a.author_id=b.author_id
where b.char_len>=100
)c
group by level_cut
order by num desc

发表于 2024-04-01 10:23:56 回复(0)
我认为题意有问题:假设某个级别的用户中没有高质量答题或则没有答题过,那么这个级别的用户答题数量就是0,按照系统正确的代码是展示不出这部分的。
发表于 2024-02-19 14:31:04 回复(0)
with t1 as(select 
    case when author_level <=2 then '1-2级'
    when author_level <=4 then '3-4级'
    when author_level <=6 then '5-6级' end as level_cut
    ,issue_id
from author_tb join answer_tb
using(author_id)
where char_len >=100)

select 
    level_cut
    ,count(issue_id) as num
from t1
group by level_cut
order by num desc

发表于 2023-11-30 11:07:24 回复(0)
select
(select (
    case  when author_level <=2 then '1-2级'
    when author_level <=4 then '3-4级'
    when author_level <=6 then '5-6级' end
)
) level_cut,
count(issue_id) num
from author_tb join answer_tb using(author_id)
where char_len >=100
group by level_cut
order by num desc
发表于 2023-11-04 17:21:40 回复(0)
有点歧义
select case when author_level>=5 then '5-6级'
            when author_level>=3 then '3-4级'
            else '1-2级' end as level_cut,
count(*) as num    #统计的是答题数,不是用户数,也不是题目的类别
from answer_tb left join author_tb using(author_id)
where char_len>=100
group by level_cut
order by num desc


发表于 2023-10-10 10:31:40 回复(0)
select
    level_cut,
    count(level_cut) as num
from
    (
        select
            author_id,
            if (
                author_level >= 5
                and author_level <= 6,
                "5-6级",
                if (
                    author_level >= 3
                    and author_level <= 4,
                    "3-4级",
                    "1-2级"
                )
            ) as level_cut
        from
            author_tb
    ) as a
    inner join (
        select
            author_id
        from
            answer_tb
        where
            char_len >= 100
    ) as t on t.author_id = a.author_id
group by
    level_cut
order by 
    num desc

发表于 2023-09-12 10:06:59 回复(0)
with tempt as(
    select n.author_id,author_level
    from answer_tb n left join author_tb u 
    on n.author_id=u.author_id
    where char_len>=100
)
select level_cut,num
from(
select '5-6级' as level_cut,
count(author_id) as num
from tempt 
where author_level in(5,6)
union 
select '3-4级' as level_cut,
count(author_id)  as num
from tempt 
where author_level in(3,4)
union 
select '1-2级' as level_cut,
count(author_id) as num
from tempt 
where author_level in(1,2)) as t
where num <>0
order by num desc,level_cut asc

发表于 2023-08-21 20:10:17 回复(0)
select (
    case when lev between 1 and 2 then '1-2级'
         when lev between 3 and 4 then '3-4级'
         else '5-6级'
    end
) as level_cut,sum(num) as num from (
select a.author_level lev,count(temp.issue_id) num from author_tb a right join (select author_id, issue_id from answer_tb where char_len>100) temp on a.author_id=temp.author_id 
group by a.author_level) as level_t
group by level_cut
order by num desc
# 注意求的是回答的数量,不是用户数量,

发表于 2023-08-14 12:13:37 回复(0)
  • 二维码

    扫描二维码,关注牛客网

  • 二维码

    下载牛客APP,随时随地刷题