I have a table say "Packages".
packageid FlightType
230 Common
231 B717
232 A330
233 B717
234 Common
235 B767
236 A330
237 A330
238 A330
239 A330
240 B767
241 B767
242 B767
Using SELECT Max(PackageID) AS PackageID, FlightType FROM Packages GROUP BY FlightType
, I am able to get the maximum packageid for each FlightType as below
PackageID FlightType
239 A330
233 B717
242 B767
234 Common
But my requirement is a SQL that would return third highest Packageid for each flightType. Here common is used in all the flight types. So It should be counted for each flight type, while returning the value. e.g. for FlightTYpe B717 maximum PackageID will be 234 and hence the 3rd largest will be 231.Over all for given table the output of the query should be
PackageID FlightType
237 A330
231 B717
240 B767
Note:-I don't require the output to have the 3rd highest packageid for 'common'. because It denotes the package which is common to all the FlightTypes.