sql面试题之”找出使用相同ip的用户“

现有一张表,里面有三个字段为user_id、ip、log_time,现有需求要找出用户共同使用ip数量大于等于3个的用户对找出来。

1.表数据准备

bash 复制代码
--建表语句  
  create table dms.user_login_log(
  user_id   string 
  ,ip       string
  ,log_time string
  );
--插入数据  
  insert into dms.user_login_log values
(102,'192.168.10.101','2022-05-10 11:04:30'),
(102,'192.168.10.102','2022-05-10 11:05:00'),
(102,'192.168.10.103','2022-05-10 11:06:00'),
(102,'192.168.10.104','2022-05-10 11:07:00'),  
(101,'192.168.10.101','2022-05-10 11:00:00'),
(101,'192.168.10.101','2022-05-10 11:01:00'),
(101,'192.168.10.102','2022-05-10 11:02:00'),
(101,'192.168.10.103','2022-05-10 11:03:00'),
(101,'192.168.10.104','2022-05-10 11:04:00'),

(103,'192.168.10.102','2022-05-10 11:08:00'),
(103,'192.168.10.103','2022-05-10 11:08:00'),
(103,'192.168.10.104','2022-05-10 11:10:00'),

(104,'192.168.10.103','2022-05-10 11:11:00'),
(104,'192.168.10.104','2022-05-10 11:12:00'),

(105,'192.168.10.105','2022-05-10 11:13:00')

2.需求分析

我们最终想要获取的是公共使用ip数量超过3的用户对,比如:101和102 共用了4个IP,101和103公用了3个IP,102和103公用3个。可以通过自关联实现。

3.实现sql如下

bash 复制代码
 select  
        A1.user_id
       ,A2.user_id
       ,count(1)
      from 
(select
      user_id
      ,ip 
 from dms.user_login_log
group by user_id,ip)  A1
join
(select
     user_id
     ,ip 
 from dms.user_login_log
group by user_id,ip) A2 
ON A1.ip = A2.ip
where A1.user_id>A2.user_id
group by A1.user_id,A2.user_id
having count(1)>=3

实现效果如下:

相关推荐
RestCloud1 天前
SQL Server到Hive:批处理ETL性能提升30%的实战经验
数据库·api
RestCloud1 天前
为什么说零代码 ETL 是未来趋势?
数据库·api
ClouGence1 天前
CloudCanal + Paimon + SelectDB 从 0 到 1 构建实时湖仓
数据库
DemonAvenger2 天前
NoSQL与MySQL混合架构设计:从入门到实战的最佳实践
数据库·mysql·性能优化
AAA修煤气灶刘哥2 天前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql
RestCloud2 天前
揭秘 CDC 技术:让数据库同步快人一步
数据库·api
得物技术2 天前
MySQL单表为何别超2000万行?揭秘B+树与16KB页的生死博弈|得物技术
数据库·后端·mysql
可涵不会debug3 天前
【IoTDB】时序数据库选型指南:工业大数据场景下的技术突围
数据库·时序数据库
ByteBlossom3 天前
MySQL 面试场景题之如何处理 BLOB 和CLOB 数据类型?
数据库·mysql·面试