Коррелированные подзапросы работ
;with batplayerlevelyear AS (
select 121 pid, 2008 season, 3 levelid union all
select 121 pid, 2008 season, 4 levelid union all
select 121 pid, 2008 season, 5 levelid union all
select 121 pid, 2009 season, 1 levelid union all
select 121 pid, 2009 season, 2 levelid union all
select 121 pid, 2010 season, 1 levelid union all
select 122 pid, 2009 season, 4 levelid union all
select 122 pid, 2009 season, 3 levelid union all
select 122 pid, 2010 season, 3 levelid union all
select 122 pid, 2011 season, 2 levelid
),
levels AS (
select 1 levelid, 'MLB' abbrev union all
select 2 levelid, 'AAA' abbrev union all
select 3 levelid, 'AA' abbrev union all
select 4 levelid, 'A+' abbrev union all
select 5 levelid, 'A' abbrev union all
select 6 levelid, 'A-' abbrev union all
select 7 levelid, 'R' abbrev
)
select
batplayerlevelyear.pid,
batplayerlevelyear.season,
min(batplayerlevelyear.levelid) highest_level,
(select levels.abbrev from levels where levelid = min(batplayerlevelyear.levelid)) abbrev
from batplayerlevelyear
group by pid, season
order by pid