此查詢可能返回指定的結果。內聯視圖t2
返回每個系列的最大季節。內聯視圖t3
爲每個系列的每個季節返回最大的Episode。內聯視圖(派生表)的結果可以連接到原始表,以檢索與這些最大值關聯的詳細信息行。 (這假定(Series,Season,Episode)
元組是唯一的。)
SELECT t1.*
FROM myTable.SeriesEpisodes t1
JOIN (SELECT s2.Series
, MAX(s2.Season) AS Season
FROM myTable.SeriesEpisodes s2
GROUP
BY s2.Series
) t2
ON t2.Series = t1.Series
AND t2.Season = t1.Season
JOIN (SELECT s3.Series
, s3.Season
, MAX(s3.Episode) AS Episode
FROM myTable.SeriesEpisodes s3
GROUP
BY s3.Series
, s3.Season
) t3
ON t3.Series = t1.Series
AND t3.Season = t1.Season
AND t3.Episode = t1.Episode
ORDER
BY t1.Series
, t1.Season
EDIT
- 或 -
的等效結果可以與這樣的查詢返回。內聯視圖t2
返回每個系列最新季節的最新情節。
SELECT t1.*
FROM myTable.SeriesEpisodes t1
JOIN (SELECT s2.Series
, s2.Season
, MAX(s2.Episode) AS Episode
FROM myTable.SeriesEpisodes s2
JOIN (SELECT s3.Series
, MAX(s3.Season) AS Season
FROM myTable.SeriesEpisodes s3
GROUP
BY s3.Series
) t3
ON t3.Series = s2.Series
AND t3.Season = s2.Season
GROUP
BY s2.Series
, s2.Season
) t2
ON t2.Series = t1.Series
AND t2.Season = t1.Season
AND t2.Episode = t1.Episode
ORDER
BY t1.Series
, t1.Season
不會有另一個「GROUP BY(t1.seriesID)」會產生你需要的結果嗎? –
您不必限制 - 您已經按照seriesID分組,因此您永遠不會再獲得更多的行。 – LoztInSpace
沒錯,但是它給了我比每個賽季最後一集更多的東西(它命令全部關閉它們並將它們全部返回),而且我只需要爲每個單獨的seriesID需要它。 – Wingman1487