首页 > 试题广场 >

推荐内容准确的用户平均评分

[编程题]推荐内容准确的用户平均评分
  • 热度指数:70822 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
某产品2022年2月8日系统推荐内容给部分用户的数据,以及用户信息和对推荐内容的评分交叉表部分数据如下:
推荐内容表recommend_tb(rec_id-推荐信息id,rec_info_l-推荐信息标签,rec_user-推荐目标用户id,rec_time-推荐时间,如下所示:
rec_id rec_info_l rec_user rec_time
1 健身 101 2022-02-08 07:23:15
2 美妆 102 2022-02-08 07:24:15
3 体育 103 2022-02-08 07:25:15
4 美妆 103 2022-02-08 07:26:15
5 政要 104 2022-02-08 07:27:15
6 体育 104 2022-02-08 07:28:15
7 体育 105 2022-02-08 07:29:15
8 影视 106 2022-02-08 07:30:15
用户信息及评分交叉表user_action_tbuser_id-用户id,hobby_l-用户喜好标签,score-综合评分),如下所示:
注:该表score为对所有推荐给该用户的内容的综合评分,在计算用户平均评分切勿将推荐次数作为分母
user_id hobby_l score
101 健身 88
102 影视 81
103 美妆 78
104 健身 68
105 体育 90
106 影视 82

问题:请统计推荐内容准确的用户平均评分?(结果保留3位小数)
注:(1)准确的定义:推荐的内容标签与用户喜好标签一致;如推荐多次给同一用户,有一次及以上准确就归为准确。
示例数据结果如下:
avg_score
84.500
解释:一共推荐8条内容,其中推荐给101、103、105、106四位用户的内容准确,
四位用户的评分分别是88、78、90、82,故平均评分=(88+78+90+82)/4=84.500
(2)如果同一用户推荐同一个内容标签的话,计算的时候只算一次。
示例1

输入

drop table if exists  `recommend_tb` ; 
CREATE TABLE `recommend_tb` (
`rec_id` int(11) NOT NULL,
`rec_info_l` varchar(8) NOT NULL,
`rec_user` int(11) NOT NULL,
`rec_time` datetime NOT NULL,
PRIMARY KEY (`rec_id`));
INSERT INTO recommend_tb VALUES(1,'健身',101,'2022-02-08 07:23:15');
INSERT INTO recommend_tb VALUES(2,'美妆',102,'2022-02-08 07:24:15');
INSERT INTO recommend_tb VALUES(3,'体育',103,'2022-02-08 07:25:15');
INSERT INTO recommend_tb VALUES(4,'美妆',103,'2022-02-08 07:26:15');
INSERT INTO recommend_tb VALUES(5,'政要',104,'2022-02-08 07:27:15');
INSERT INTO recommend_tb VALUES(6,'体育',104,'2022-02-08 07:28:15');
INSERT INTO recommend_tb VALUES(7,'体育',105,'2022-02-08 07:29:15');
INSERT INTO recommend_tb VALUES(8,'影视',106,'2022-02-08 07:30:15');

drop table if exists  `user_action_tb` ;   
CREATE TABLE `user_action_tb` (
`user_id` int(11) NOT NULL,
`hobby_l` varchar(8) NOT NULL,
`score` int(11) NOT NULL,
PRIMARY KEY (`user_id`));
INSERT INTO user_action_tb VALUES(101,'健身',88);
INSERT INTO user_action_tb VALUES(102,'影视',81);
INSERT INTO user_action_tb VALUES(103,'美妆',78);
INSERT INTO user_action_tb VALUES(104,'健身',68);
INSERT INTO user_action_tb VALUES(105,'体育',90);
INSERT INTO user_action_tb VALUES(106,'影视',82);

输出

84.500
select
avg(u.score) as avg_score
from recommend_tb  r
left join user_action_tb u
on r.rec_user=u.user_id
where r.rec_info_l=u.hobby_l 为啥自测能通过,提交通不过
发表于 2025-11-30 17:20:32 回复(0)
with t1 as (
    select distinct user_id, score
    from recommend_tb r
    inner join user_action_tb u
    on rec_user = user_id and rec_info_l = hobby_l
)

select round(sum(score) / count(*), 3) as avg_score
from t1; 
发表于 2025-10-30 21:45:25 回复(0)
select 
round(sum(distinct uat.score)/count(distinct uat.user_id),3) as avg_score
from user_action_tb as uat
join recommend_tb as rt
on rt.rec_user = uat.user_id
where rt.rec_info_l = uat.hobby_l
group by uat.user_id and rt.rec_info_l

发表于 2025-10-22 16:09:03 回复(0)
select
round(avg(u.score), 3)
from (
    select distinct rec_info_l, rec_user from recommend_tb
) r join user_action_tb u
on r.rec_user = u.user_id and r.rec_info_l = u.hobby_l

发表于 2025-10-19 21:45:13 回复(0)
with t1 as (
select user_id
from user_action_tb  x
join recommend_tb y
on x.user_id=y.rec_user
group by 1
having sum(if(rec_info_l=hobby_l,1,0))>=1
)
select round(avg(score),3) as avg_score
from user_action_tb
where user_id in (select * from t1)

发表于 2025-10-16 12:03:00 回复(0)
select round(avg(score),3) as avg_score
from user_action_tb
where user_id in (
select rec_user
from recommend_tb r inner join user_action_tb u on r.rec_user=u.user_id
and rec_info_l=hobby_l) 
榜一的清晰啊
发表于 2025-08-25 22:59:08 回复(0)
select avg(only) from (
select
a.user_id,a.hobby_l,max(score) as  only
from user_action_tb a
left join recommend_tb b
on a.user_id=b.rec_user
and a.hobby_l=b.rec_info_l
where b.rec_id is not null
group by a.user_id,a.hobby_l
)t
发表于 2025-08-21 16:50:41 回复(0)
select sum(distinct u.score)/count(distinct user_id) as avg_score
from user_action_tb u
join recommend_tb r
on u.hobby_l=r.rec_info_l and u.user_id=r.rec_user
取巧了,正好分数没有重复的,不然通不过
发表于 2025-07-17 10:19:53 回复(0)
with
    t1 as (
        select distinct
            rec_info_l,
            rec_time,
            hobby_l,
            score
            ,rec_user
        from
            recommend_tb r
            left join user_action_tb u on r.rec_user = u.user_id
        where
            rec_info_l = hobby_l
    )
select
   avg(score)
from
    t1
为什么写出来取到的id和分数都对,但是avg(score)就不对了,求问一下大佬我这里哪里出错了

发表于 2025-06-11 13:07:21 回复(0)
SELECT
    round(avg(score), 3) AS avg_score
FROM
    (
        SELECT
            t1.rec_user,
            max(
                CASE
                    WHEN t1.rec_info_l = t2.hobby_l THEN 1
                    ELSE 0
                END
            ) AS flag,
            t2.score
        FROM
            recommend_tb t1
            JOIN user_action_tb t2 ON t1.rec_user = t2.user_id
        GROUP BY
            t1.rec_user,
            t2.score
    ) t3
WHERE
    flag = 1

发表于 2025-05-02 13:58:42 回复(0)
select (select sum(score) from user_action_tb ua
where user_id in(select rec_user from recommend_tb rt
where  ua.hobby_l=rt.rec_info_l and ua.user_id=rt.rec_user
 ))
 /
 (select count(distinct rec_user) from recommend_tb rt
inner join user_action_tb  ua
on ua.hobby_l=rt.rec_info_l and ua.user_id=rt.rec_user) as avg_score;
发表于 2025-01-23 21:08:56 回复(0)
-- 求大佬解答,为啥要加distinct,不理解,为啥自测运行正常,保存提交就数字对不上
select avg(distinct score)
from recommend_tb
join user_action_tb
on rec_user=user_id
where rec_info_l=hobby_l
发表于 2024-11-17 20:28:02 回复(0)
select avg(a.score) from (select distinct u.user_id,score from
recommend_tb as r inner join user_action_tb as u on r.rec_info_l=u.hobby_l and r.rec_user=u.user_id) as a
发表于 2024-10-29 17:24:18 回复(0)