我正在为视频重播网站创建一个数据库,我有一个包含用户和表格的表格,其中包含观看历史记录。我需要找到几个最受瞩目的视频对使用SQL查询。例子:用户1观看视频12,43,50,66,78;用户2观看了12,43,45,50;用户3观看了12,35,50,66,78;用户4观看了33,66,69,78 所以最受欢迎的两对夫妇是(12,50)和(66,78)。 但我甚至无法知道如何组建这对情侣以备将来计算。 所以,我的问题是如何形成所有可能的夫妻并计算他们每个人的观点数量。
答案 0 :(得分:0)
在下面的解决方案中,我创建了一个子查询来模拟输入数据。在您的应用中,您应该使用观看历史记录表,而不是viewing_history
。我不知道"用户"表与此问题相关。我命名为movie_pairs
的第二个子查询是查看历史记录与其自身的内部联接 - 这是您创建对的方式。我超越了这一点 - 随着这一切,我继续确定最常见的对。
with
viewing_history ( userid, movie ) as (
select 1, 12 from dual union all
select 1, 43 from dual union all
select 1, 50 from dual union all
select 1, 66 from dual union all
select 1, 78 from dual union all
select 2, 12 from dual union all
select 2, 43 from dual union all
select 2, 45 from dual union all
select 2, 50 from dual union all
select 3, 12 from dual union all
select 3, 35 from dual union all
select 3, 50 from dual union all
select 3, 66 from dual union all
select 3, 78 from dual union all
select 4, 33 from dual union all
select 4, 66 from dual union all
select 4, 69 from dual union all
select 4, 78 from dual
),
-- end test data, query begins here (but include the keyword WITH from above)
movie_pairs ( movie1, movie2, ct ) as (
select a.movie, b.movie, count(*)
from viewing_history a inner join viewing_history b
on a.userid = b.userid and a.movie < b.movie
group by a.movie, b.movie
)
select movie1, movie2
from movie_pairs
where ct = (select max(ct) from movie_pairs)
order by movie1, movie2 -- ORDER BY is optional
;
<强>输出强>:
MOVIE1 MOVIE2
---------- ----------
12 50
66 78
答案 1 :(得分:0)
自我join
是执行此操作的正确方法。我认为最简单的查询形式是:
select vh.*
from (select vh1.movie as movie1, vh2.movie as movie2, count(*) as cnt,
rank() over (order by count(*) desc) as seqnum
from viewing_history vh1 inner join
viewing_history vh2
on vh1.userid = vh2.userid and vh1.movie < vh2.movie
group by vh1.movie, vh2.movie
) vh
where seqnum = 1;