力扣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 ;
相关推荐
ltl9 分钟前
数据库作为 LLM 记忆体:语义缓存、RAG 与一致性
数据库
ltl12 分钟前
WAL 与崩溃恢复:ARIES 协议怎么跑通
数据库
ltl25 分钟前
TEE 数据库:EnclaveDB、Oblivious 原语与机密 SQL
数据库
白狐_79839 分钟前
408数据结构第8章:排序②——性质对比秒杀、场景选择与外部排序
java·数据结构·算法
zander2581 小时前
LeetCode 84:柱状图中的最大矩形——单调栈如何确定左右边界
java·数据结构·算法
Shell运维手记1 小时前
Linux 常用基础命令学习笔记
linux·运维·笔记·学习·算法·github
麻瓜code2 小时前
【Mysql】重新学一遍 SQL 执行顺序
数据库·sql
杨航 AI2 小时前
O(n log n):线性对数原理拆解 这个是排序算法的黄金复杂度之一。
算法·排序算法
这个DBA有点耶2 小时前
数据库一体机架构演进:从硬件堆叠到软硬深度耦合
服务器·网络·数据库·硬件架构·运维开发·database·数据库架构
一只fish2 小时前
优化器架构对比:Oracle vs PostgreSQL vs MySQL
mysql·postgresql·oracle