力扣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 ;
相关推荐
田里的水稻13 分钟前
FA_规划和控制(PC)-瑞德斯.谢普路径规划(RSPP))
人工智能·算法·数学建模·机器人·自动驾驶
罗湖老棍子19 分钟前
【例 1】二叉苹果树(信息学奥赛一本通- P1575)
算法·树上背包·树型动态规划
heimeiyingwang21 分钟前
大模型 RAG 技术原理与企业级落地实践
大数据·数据库·人工智能·架构
元亓亓亓1 小时前
LeetCode热题100--76. 最小覆盖子串--困难
算法·leetcode·职场和发展
CHANG_THE_WORLD1 小时前
C++数组地址传递与数据影响:深入理解指针与内存
算法
json{shen:"jing"}1 小时前
力扣-单词拆分
数据结构·算法
aaa7871 小时前
Codeforces Round 1080 (Div. 3) 题解
数据结构·算法
浮生09192 小时前
DHUOJ 基础 85 86 87
数据结构·c++·算法
倔强的石头_2 小时前
【金仓数据库】ksql 指南(七) —— 启动和管理事务(KingbaseES 数据一致性保障)
数据库
say_fall2 小时前
双指针算法详解:从原理到实战(含LeetCode经典例题)
算法·leetcode·职场和发展