views:

82

answers:

4

Hi guys..

The question I'm struggling with is this:

I have a list of helicopter names in different charters and I need to find out WHICH helicopter has the least amount of charters booked. Once I find that out I need to ONLY display the one that has the least.

I so far have this:

SELECT Helicopter_Name 
       , COUNT (Distinct Charter_NUM)
FROM Charter_Table
GROUP BY Helicopter Name

This is where I am stuck. I realise MIN could be used to pick out the value that is the smallest but I am not sure how to integrate this into the command.

Something like Where MIN = MIN Value

I'd really appreciate it.

A: 

Oh just thought of something could i add this:

(for example just say theres 10 charters and helicopter RED had the least)

SELECT Helicopter_Name COUNT (Distinct Charter_NUM) FROM Charter_Table GROUP BY Helicopter Name HAVING Total = MIN

Would this bring forward Helicopter RED's value?

Mila
A: 

This is the simple answer.

SELECT Helicopter_Name
FROM Charter_Table 
GROUP BY Helicopter_Name
HAVING COUNT (Distinct Charter_NUM) = 
   (SELECT MIN(COUNT (Distinct Charter_NUM)) CNT
   FROM Charter_Table 
   GROUP BY Helicopter_Name)
Gary
THANK YOU!!!!!!!!!! I very much appreciate it!
Mila
+1  A: 

One thing you need to consider is, what happens if two or more helicopters have the least number of charters booked?

If you just want to "pick one", you can simply sort by the count (descending) and report the first result:

SELECT * FROM (
   SELECT Helicopter_Name, c FROM (
      SELECT Helicopter_Name, COUNT (Distinct Charter_NUM) AS c
      FROM Charter_Table GROUP BY Helicopter 
   ) ORDER BY c DESC
) WHERE ROWNUM = 1;

Alternatively, if you need to report all the helis that have the least number of charters taking ties into account, you can use the RANK analytic function:

SELECT Helicopter_Name, c FROM (
   SELECT Helicopter_Name, c, RANK() OVER (ORDER BY c) therank FROM (
      SELECT Helicopter_Name, COUNT (Distinct Charter_NUM) AS c
      FROM Charter_Table GROUP BY Helicopter 
   )
) WHERE therank = 1;
Jeffrey Kemp
Ohh thank you so much! I'm sorry to ask it may sound rude but do you have an email i could contact you through? You seem to be very good with SQL and it would be good to have someone to converse with about it, i'm literally the only girl in my classes and i don't have ANYONE i can talk to to discuss this work, i find it very difficult. This is my email if you ever have spare time:[email protected] you again! I appreciate your reply so much!
Mila
I keep an eye on stackoverflow for questions tagged "oracle"; and there are plenty of great SQL people on here. Post your questions here, that way other people such as yourself can benefit as well.
Jeffrey Kemp
thank you! :)).
Mila
+1  A: 

Here's a solution that just takes the first row, after ordering them lowest count to highest count:

SELECT *
FROM (
    SELECT *, ROW_NUMBER() OVER (ORDER BY Charter_Count) AS RN
    FROM (
        SELECT Helicopter_Name, COUNT(Distinct Charter_NUM) Charter_Count
        FROM Charter_Table GROUP BY Helicopter_Name
    ) AS t1
) AS t2
WHERE t2.RN = 1;

Here's a solution that uses HAVING to compare the count of a given group to the lowest count given all the groups. Based on answer given by @Gary but adding another level of subquery.

SELECT Helicopter_Name, COUNT(Distinct Charter_NUM) AS Charter_Count
FROM Charter_Table 
GROUP BY Helicopter_Name
HAVING COUNT(Distinct Charter_NUM) = (SELECT MIN(C) FROM
    (SELECT COUNT(Distinct Charter_NUM) AS C
     FROM Charter_Table 
     GROUP BY Helicopter_Name))
Bill Karwin