声明:“已购买全系列许可证的客户”
表:
SERIES:
title
total_seasons
SEASONS:
title
season
avgduration
episodes
PRIMARY KEY (title, season),
FOREIGN KEY (title) REFERENCES SERIES
LIC_SERIES:
id_client
datetime
title
season
episode
PRIMARY KEY (client,title,season,episode)
FOREIGN KEY (title,season) REFERENCES seasons
系列示例如下所示:
Title Total_Seasons
House 8
Lost 6
季节示例如下所示:
Title Season AvgDuration Episodes
House 1 44 22
Mad Men 3 47 13
Lic_Series示例如下所示:
Client Datetime Title Season Episode
92/657/T 01/01/2017 House 8 18
我尝试了什么?
WITH PART AS (SELECT DISTINCT EPISODES, TITLE
FROM SEASONS INNER JOIN SERIES
ON Seasons.Title = Series.Title
GROUP BY Seasons.Title)
SELECT CLIENT
FROM LIC_SERIES INNER JOIN PART
ON LIC_SERIES.TITLE = PART.TITLE
ORDER BY title DESC;
我不知道如何检查客户是否有每季的所有剧集,以及该系列剧的所有季节
答案 0 :(得分:0)
这将返回每个季节拥有且不属于每个客户的剧集数量。要检查是否拥有所有剧集,请对episodes_not_owned为零的摘要进行额外检查
WITH PART AS
(SELECT DISTINCT s2.TITLE, s1.SEASON, s2.EPISODE
FROM SEASONS s1
INNER JOIN SERIES s2
ON s1.Title = s2.Title
)
,summary as (
SELECT l.CLIENT
,p.SEASON
,SUM(CASE WHEN l.episode is not null THEN 1 ELSE NULL END) as episodes_owned
,SUM(CASE WHEN l.episode is null THEN 1 ELSE NULL END) as episodes_not_owned
FROM LIC_SERIES l
RIGHT OUTER JOIN PART p
ON l.TITLE = p.TITLE
AND l.EPISODE = p.EPISODE
AND l.SEASON = p.SEASON
GROUP BY l.CLIENT, p.SEASON
ORDER BY l.CLIENT, p.SEASON DESC
)
SELECT s.client
,s.season
,s.episodes_owned
,s.episodes_not_owned
,case when s.episodes_not_owned = 0 THEN 'Yes' ELSE 'No' END as all_owned
from summary s;