力扣1809 没有广告的剧集(postgresql)

需求

Table: Playback

±------------±-----+

| Column Name | Type |

±------------±-----+

| session_id | int |

| customer_id | int |

| start_time | int |

| end_time | int |

±------------±-----+

该表主键为:session_id (剧集id)

customer_id 是观看该剧集的观众id

剧集播放时间包含start_time(开始时间) 及 end_time(结束时间)

可以保证的是,start_time(开始时间)<= end_time(结束时间),一个观众观看的两个剧集的时间不会出现重叠。

Table: Ads

±------------±-----+

| Column Name | Type |

±------------±-----+

| ad_id | int |

| customer_id | int |

| timestamp | int |

±------------±-----+

该表的主键为:ad_id(广告id)

customer_id 为 观看广告的用户id

timestamp 表示广告出现的时间点

请查出,所有没有广告出现过的剧集。

如果观众观看了剧集,并且剧集里出现了广告,就一定会有观众观看广告的记录。

返回结果没有顺序要求。

输入

输出

sql 复制代码
with t1 as (
-- 两表左外连接,筛选出广告时间在剧集时间之外的数据
select session_id,a.customer_id,start_time,end_time,ad_id,timestamp
from playback p left join ads a
on p.customer_id = a.customer_id
and (timestamp<start_time or timestamp>end_time)
)
-- 排除掉上步查询结果为空的数据,对求出来的剧集id去重
select distinct session_id
from t1
where timestamp notnull ;
相关推荐
jianfeng_zhu1 天前
二叉排序树的建立和插入
算法
Go高并发架构_王工1 天前
Redis未来展望:Redis 7.0新特性与技术发展趋势
数据库·redis·缓存
不会c嘎嘎1 天前
数据结构 -- 常见的八大排序算法
数据结构·c++·算法·排序算法·面试题·快速排序
WW_千谷山4_sch1 天前
洛谷P8653:[模板] [蓝桥杯 2017 国 C] 分考场(染色最小色数)
c++·算法·蓝桥杯·深度优先
产幻少年1 天前
用户登录日志表和系统日志
运维·服务器·数据库
·云扬·1 天前
InnoDB Cluster高可用测试实战:主从切换与故障恢复验证
数据库·mysql
hk11241 天前
【NLP/PatternRec】2026年度语义鸿沟分析与模糊模式识别基准索引 (Benchmark Index)
算法·自然语言处理·数据集·知识图谱·模式识别
hetao17338371 天前
2025-12-31~2026-1-2 hetao1733837 的刷题笔记
c++·笔记·算法
qq_455760851 天前
redis - 持久化
数据库·redis·缓存
&友情岁月&1 天前
sql脚本的union的要注意点
数据库·sql