力扣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 ;
相关推荐
chao1898442 分钟前
MATLAB 实现声纹识别特征提取
人工智能·算法·matlab
zhishidi4 分钟前
推荐算法之:GBDT、GBDT LR、XGBoost详细解读与案例实现
人工智能·算法·推荐算法
货拉拉技术5 分钟前
货拉拉RAG优化实践:从原始数据到高质量知识库
数据库·算法
张较瘦_14 分钟前
数据库|数据库设计范式:用“宠物管理系统“讲透1nf 2nf 3nf的关键逻辑
数据库·开发
源来猿往16 分钟前
并发之锁介绍
数据库
AKDreamer_HeXY16 分钟前
ABC434E 题解
c++·算法·图论·atcoder
罗湖老棍子16 分钟前
完全背包 vs 多重背包的优化逻辑
c++·算法·动态规划·背包
TL滕16 分钟前
从0开始学算法——第四天(题目参考答案)
数据结构·笔记·python·学习·算法
路边草随风19 分钟前
starrocks compaction 进度问题定位
大数据·sql
potato_may26 分钟前
C++ 发展简史与核心语法入门
开发语言·c++·算法