--成功抽取出的 ,原问题所要的结果 : --显示是用的select *, 所以如果要完美的话,后续可以在display的列方面多加些选择和考虑!!!
select
c.AthleteId,
c.Athlete as TopAnnaulAthleteSolo,
(3*Gold_Medals + 2*Silver_Medals + Bronze_Medals) as Max_Medal_Weight,
CONVERT(VARCHAR(10), Closing_Ceremony_Date, 101) as FormatDate,
c.OlympicYear,
c.Country
from OlympicAthletes as c
where c.Athlete = (
select top 1 b.TopAnnaulAthleteSolo
from
(
select
main.AthleteId,
main.Athlete as TopAnnaulAthleteSolo,
(3*Gold_Medals + 2*Silver_Medals + Bronze_Medals) as Max_Medal_Weight,
main.OlympicYear,
main.Country
from OlympicAthletes as main
where (3*Gold_Medals + 2*Silver_Medals + Bronze_Medals) = (
select Max (3*a.Gold_Medals + 2*a.Silver_Medals + a.Bronze_Medals) as MaxMedal_Weight
from OlympicAthletes as a
where main.OlympicYear =a.OlympicYear and main.Country = a.Country
)
) as b
where c.OlympicYear =b.OlympicYear and c.Country = b.Country and (3*Gold_Medals + 2*Silver_Medals + Bronze_Medals) = b.Max_Medal_Weight
)
order by c.OlympicYear,c.Country
--//最后合并出 ,基本符合 完整需求的sql 代码:
select *
from
(
select
c.AthleteId,
c.Athlete as TopAnnaulAthleteSolo,
(3*Gold_Medals + 2*Silver_Medals + Bronze_Medals) as Max_Medal_Weight,
CONVERT(VARCHAR(10), Closing_Ceremony_Date, 101) as FormatDate,
c.OlympicYear,
c.Country
from OlympicAthletes as c
where c.Athlete = (
select top 1 b.TopAnnaulAthleteSolo
from
(
select
main.AthleteId,
main.Athlete as TopAnnaulAthleteSolo,
(3*Gold_Medals + 2*Silver_Medals + Bronze_Medals) as Max_Medal_Weight,
main.OlympicYear,
main.Country
from OlympicAthletes as main
where (3*Gold_Medals + 2*Silver_Medals + Bronze_Medals) = (
select Max (3*a.Gold_Medals + 2*a.Silver_Medals + a.Bronze_Medals) as MaxMedal_Weight
from OlympicAthletes as a
where main.OlympicYear =a.OlympicYear and main.Country = a.Country
)
) as b
where c.OlympicYear =b.OlympicYear and c.Country = b.Country and (3*Gold_Medals + 2*Silver_Medals + Bronze_Medals) = b.Max_Medal_Weight
)
) as d
inner join
(
select
sum(Gold_Medals) as Total_GM,
sum(Silver_Medals)as Total_SM,
sum(Bronze_Medals) as Total_BM,
sum(Gold_Medals + Silver_Medals + Bronze_Medals) as Total_Medals,
OlympicYear,
Country
from OlympicAthletes
group by OlympicYear,Country
) as e
on d.OlympicYear = e.OlympicYear and d.Country = e.Country
order by d.OlympicYear,d.Country