Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
For each country, find the year, in which the maximal number of ships had been launched.In the case of more than one year in question, take a minimal year. Result set: country, number of ships, yearclasses ships======== ========class(pri key) name(name of the ship)pri keycountry class(ship class name) foreign key ref launched(year the ship launched) relation:1->M i have tried the below query,but i think this is not the correctselect country,count(name) as qty,year(launched)as yearfrom ships s join classes con s.class=c.classgroup by country,year(launched)
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2007-12-19 : 04:54:47
Try this:-
SELECT tmp.Country,tmp.ShipCount,tmp.Year FROM( SELECT ROW_NUMBER() OVER (PARTITION BY t.Country ORDER BY ShipCount DESC) AS 'RowNo', t.Country, t.Year, t.shipcount FROM ( SELECT c.Country, YEAR(s.launched) AS 'Year', Count(s.name) AS 'ShipCount' FROM classes c INNER JOIN ships s ON c.class=s.class GROUP BY c.Country,YEAR(s.launched) )t)tmpWHERE tmp.Row_No=1
khtan
In (Som, Ni, Yak)
17689 Posts
Posted - 2007-12-19 : 04:55:29
Another homework ?KH[spoiler]Time is always against us[/spoiler]