形成并计算最常出现的一对 sql oracle
Form and count the most frequented pair sql oracle
我正在为视频回放站点创建一个数据库,我有一个 table 用户和 table 观看历史记录。我需要使用 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)。
但我什至不知道如何为未来的计数形成这对夫妇。
所以,我的问题是如何组成所有可能的配对并计算每个配对的观看次数。
在下面的解决方案中,我创建了一个子查询来模拟输入数据。在您的应用程序中,您应该使用您的观看历史记录 table 而不是 viewing_history
。我看不出 "users" table 与这个问题有什么关系。第二个子查询,我命名为 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
自我 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;
我正在为视频回放站点创建一个数据库,我有一个 table 用户和 table 观看历史记录。我需要使用 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)。 但我什至不知道如何为未来的计数形成这对夫妇。 所以,我的问题是如何组成所有可能的配对并计算每个配对的观看次数。
在下面的解决方案中,我创建了一个子查询来模拟输入数据。在您的应用程序中,您应该使用您的观看历史记录 table 而不是 viewing_history
。我看不出 "users" table 与这个问题有什么关系。第二个子查询,我命名为 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
自我 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;