力扣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 ;
相关推荐
吞下星星的少年·-·3 分钟前
The 2026 ICPC Asia East Continent Online Contest (II)(构造)
数据结构·算法
李高钢32 分钟前
Python FastAPI 框架入门:从零搭建你的第一个高性能 API 服务
数据库·python·fastapi
stolentime1 小时前
洛谷P10515 转圈题解
c++·算法·数学建模·贪心算法
yuzhiboyouye2 小时前
那xml对应的sql语法,列举一下
xml·数据库·sql
和裕2 小时前
蜂窝板 vs 七层瓦楞重型纸箱:大件工业设备运输性能与成本全对比
大数据·运维·网络·人工智能·算法
. . . . .3 小时前
乐观锁 vs 悲观锁
数据库·sql
衡石科技3 小时前
Data_Agent记忆机制与经验复用衡石分析智能体会话记忆与长期学习技术解析
人工智能·科技·学习·算法·企业级bi
Leo.yuan3 小时前
2026年多数据库实时同步的四种架构方案及工具推荐
数据库·架构
hasty3 小时前
Origin 校验不是身份认证:MySQL MCP Server 漏洞给 AI 平台的警告
数据库·mysql·安全
shirsl4 小时前
算法 Day 2 滑动窗口 + 栈 / 单调栈
开发语言·python·算法