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.
| Author |
Topic |
|
Jackson
Starting Member
6 Posts |
Posted - 2008-05-27 : 02:05:03
|
| Hi, I am a new learner and user to SQL Sever 2005 and am having some major frustration trying to write a simple query.I have two tables, 1) Ticket_Purchase, 2) Flight.The Ticket_Purchase table has these columns: Ticket_Purchase_Number(PK), Flight_Number(FK), Date_Purchase_Made, Ticket_Price, Class_of_Ticket, Passenger_IDThe Flight table has these columns: Flight_Number(PK), Flight_Date, Flight_Departure_Time, Flight_Arrival_Time, Flight_Origin, Flight_DestinationI am trying to create a query that will tell me: On which flight were the most first class tickets sold?There are only two types of classes; 'E' for economy and 'F' for First Class.So far I am able to get a list of all the First class flights for each flight and can visually see which flight has the most first class tickets by counting them manually on the report generated, but I am totally confused on how to simply pull the single flight with the most First class tickets sold. I wonder if this requires something more like a join or a nested sub query?The SQL I wrote for the above is:Select Class_of_Ticket, Flight_NumberFrom Ticket_Purchase Where Class_of_Ticket = ('F')Order By Flight_Number;And it produces:Class_of_Ticket Flight_Number--------------- -------------F 1F 1F 1F 2F 2F 3F 3F 3F 3F 4F 4F 4F 4F 4F 4F 4F 4F 4F 4F 4F 4F 4F 4F 5F 5F 6F 6F 6F 7F 7F 8F 8F 8F 9F 9F 9F 9F 9(38 row(s) affected)Rather I would like it to produce:First_Class_Seats Purchased Flight_Number--------------------------- ------------14 4 I hope I didn't make this to confusing to understand as I am still learning the syntax and 'lingo' of how to communicate this stuff verbally.Thank you for any help you could offer. It would be much appreciated. Edit: the query report I pasted from SQL should have the flight number directly under the column header. For some reason the space between Class and Flight_number is being eliminated in the post. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-27 : 02:09:17
|
| [code]SELECT TOP 1 * FROM(Select COUNT(*) AS TicketCount, Flight_NumberFrom Ticket_Purchase Where Class_of_Ticket = ('F')GROUP BY Flight_Number)tOrder By t.TicketCount DESC;[/code] |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-05-27 : 02:12:31
|
And for handle ties (more than one flight number has most number of first class tickets)SELECT TOP 1 WITH TIES Flight_Number, COUNT(*) AS TicketCountFROM Ticket_PurchaseWHERE Class_of_Ticket = 'F'GROUP BY Flight_NumberORDER BY COUNT(*) DESC E 12°55'05.25"N 56°04'39.16" |
 |
|
|
Jackson
Starting Member
6 Posts |
Posted - 2008-05-27 : 02:20:21
|
| Wow, you are fast. Thank you so much!One question for the first select: SELECT TOP 1 * FROMIs it possible to write a HAVING or WHERE clause in replace of 'TOP 1 *' that would use a boolean and compare that to the nested Select and come up with the answer? Again Thanks so much. |
 |
|
|
Jackson
Starting Member
6 Posts |
Posted - 2008-05-27 : 02:23:54
|
| Sorry, my 2nd post probably didn't make any sense. I just have not used TOP before. Thank you both for the help. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-27 : 02:26:52
|
quote: Originally posted by Jackson Sorry, my 2nd post probably didn't make any sense. I just have not used TOP before. Thank you both for the help.
You're welcome |
 |
|
|
|
|
|
|
|