简体   繁体   中英

group by and max on two columns

I have a table with these columns:

id | series_id | season_id | episode_id |  title | type ...

I would like to get rows with unique series_id where season_id and episode_id are max.

One solution is to do this:

SELECT t1.*
FROM YourTable AS t1
INNER JOIN 
(
   SELECT 
     series_id, 
     MAX(season_id)  AS MAxSeasonId, 
     MAX(Episode_id) AS MAXEpisodeID
   FROM yourTable 
   GROUP BY series_id
) AS t2  ON t1.series_id  = t2.series_id
        AND t1.season_id  = t2.MaxSeasonId
        AND t1.episode_id = t2.MaxEpisode_id;
SELECT *
FROM TableName t1
WHERE EXISTS (
              SELECT 1
              FROM t2
              WHERE t1.series_id = t2.series_id
              HAVING MAX(t2.season_id) = t1.season_id
                AND MAX(t2.episode_id ) = t1.episode_id
              )

Try this..

SELECT * FROM TABLE
HAVING MAX(season_id) AND MAX(episode_id)
GROUP BY series_id

:)

I think this may be overkill, but it is the only way I have found that will work for me.

DECLARE @SeriesInfo TABLE
(
    id          INT,
    series_id   INT,
    season_id   INT,
    episode_id  INT,
    title       VARCHAR(50),
    type        CHAR(1)
);
INSERT INTO @SeriesInfo VALUES ( 1, 1, 1, 1, 'Series 1 Season 1 Episode 1', 'A'),
                               ( 2, 1, 1, 2, 'Series 1 Season 1 Episode 2', 'A'),
                               ( 3, 1, 1, 3, 'Series 1 Season 1 Episode 3', 'A'),
                               ( 4, 1, 2, 1, 'Series 1 Season 2 Episode 1', 'A'),
                               ( 5, 1, 2, 2, 'Series 1 Season 2 Episode 2', 'A'),
                               ( 6, 2, 1, 1, 'Series 2 Season 1 Episode 1', 'A'),
                               ( 7, 2, 1, 2, 'Series 2 Season 1 Episode 2', 'A'),
                               ( 8, 2, 1, 3, 'Series 2 Season 1 Episode 3', 'A'),
                               ( 9, 2, 1, 4, 'Series 2 Season 1 Episode 4', 'A'),
                               (10, 2, 2, 1, 'Series 2 Season 2 Episode 1', 'A'),
                               (11, 2, 2, 2, 'Series 2 Season 2 Episode 2', 'A'),
                               (12, 2, 2, 3, 'Series 2 Season 2 Episode 3', 'A');
SELECT  id,
        series_id,
        season_id,
        episode_id,
        title,
        type
FROM    (
         SELECT ROW_NUMBER() OVER(PARTITION BY series_id ORDER BY season_id DESC, episode_id DESC) RowNum,
                *
         FROM   @SeriesInfo
        )   X
WHERE   X.RowNum = 1;

--SELECT ROW_NUMBER() OVER(PARTITION BY series_id ORDER BY season_id DESC, episode_id DESC) RowNum, * FROM @SeriesInfo;

(Sorry for the over abundance of sample data)

The point is, if we just take the max of the series_id and season_id, we will not get a valid pair for either series. In both cases, there were more episodes in season 1 than season 2. The ROW_NUMBER() clause will return a unique number per row but because of the "PARTITION BY", it will restart for each series_id (see the commented out line). If we return only the rows with a ROW_NUMBER of 1, we will get one row per series_id and it will be the one with the max episode_id in the max season_id.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM