简体   繁体   中英

SQL programming help… using count and max ..etc

I have a series of tables:

  • TECH PERSONNEL (pplSoft, fname, lname, pittID, expertise, office phone) where fname is first name, and lname is last name.

  • USERS (pplSoft, fname, lname, pittID, office phone)

  • CATEGORIES (category id, category, description) where this table lists all possible categories of submitted tickets.

  • INVENTORY(machine name, IP, network port, MACADDR, location id)

  • LOCATIONS(location id, location, building, notes)

  • TICKETS (ticket number, owner pplSoft, date submitted, date closed, days worked on, category id, machine name, location, description)

  • ASSIGNMENT (ticket number, tech pplSoft, date assigned, status) where status held is an enumeration, could be: assigned, in progress, delegated, closed successful, or closed unsuccessful.

My task is to list the Device Name all names of the machines that had the maximum number of problems in the two months of December 2011 and January 2012.

I have to turn this into SQL.

Can I do something like this?

select machine_name 
from tickets 
where date_submitted >= '01-DEC-2012' and 'date_submitted <= '31-JAN-2012'  

But I need to count the tickets or use max?

How do I make progress from here?

As with any complex SQL query, the secret is to break it up into parts, preferably independently testable parts.

The first problem is to establish the count of the number of tickets each machine had during the period in question. What is the criterion here? Probably, if a machine had a problem that started in November 2011 and extended into December 2011, that should be counted; likewise, if a problem was started in January 2012 but completed in February 2012, that should be counted. So, we need:

SELECT machine_name, COUNT(*) AS num_tickets
  FROM tickets
 WHERE date_completed >= '01-Dec-2011' AND date_submitted <= '31-Jan-2012'
 GROUP BY machine_name;

If you decide it is only the dates when the tickets were submitted that count, adjust the criterion to reference date_submitted twice; likewise, if it is only the dates when the tickets were completed that count, then reference date_completed twice. Note that if a machine had a ticket that was started in November and not resolved until February, the query above will count it; if you use either of the alternatives, that machine had no problem during the period in question.

That tells us how many tickets were open for the machine during the time period. Now we need to find which number of tickets is the maximum number:

SELECT MAX(num_tickets) AS max_tickets
  FROM (SELECT machine_name, COUNT(*) AS num_tickets
          FROM tickets
         WHERE date_completed >= '01-Dec-2011' AND date_submitted <= '31-Jan-2012'
         GROUP BY machine_name
       );

Now we need to select the machine name(s) that had this number of tickets:

SELECT machine_name
  FROM (SELECT MAX(num_tickets) AS max_tickets
          FROM (SELECT machine_name, COUNT(*) AS num_tickets
                  FROM tickets
                 WHERE date_completed >= '01-Dec-2011' AND date_submitted <= '31-Jan-2012'
                 GROUP BY machine_name
               )
        ) AS n
  JOIN (SELECT machine_name, COUNT(*) AS num_tickets
          FROM tickets
         WHERE date_completed >= '01-Dec-2011' AND date_submitted <= '31-Jan-2012'
         GROUP BY machine_name
       ) AS m
    ON n.max_tickets = m.num_tickets;

Assuming Oracle supports the WITH clause, this can be simplified (considerably):

WITH t AS
    (SELECT machine_name, COUNT(*) AS num_tickets
       FROM tickets
      WHERE date_completed >= '01-Dec-2011' AND date_submitted <= '31-Jan-2012'
      GROUP BY machine_name
    )
SELECT t.machine_name
  FROM t
  JOIN (SELECT MAX(num_tickets) AS max_tickets FROM t) AS m
    ON t.num_tickets = m.max_tickets;

Caveat : I've used 'AS alias' on the sub-queries, as supported by the SQL Standard. I believe Oracle does not allow 'AS alias' and requires just 'alias' after table names; I'm not sure whether that also applies to names for sub-queries. If the 'AS m' and 'AS n' notations cause trouble, try dropping the AS. You might find a similar issue with the column renamings 'AS num_tickets' etc, but I believe Oracle does allow AS in that context.

Presumably, this is just one of a series of questions since the answer doesn't seem to require any of the tables except the Tickets table. Presumably, other questions require the use of other tables.

You need to use group by .

select machine_name, count(*) as numMachines
from tickets
where date_submitted >= '01-DEC-2011' and 'date_submitted <= '31-JAN-2012'
group by machine_name
order by numMachines desc
select machine_name, count(machine_name) as totalTicketCount
from tickets 
where date_submitted >= '01-DEC-2012' and 'date_submitted <= '31-JAN-2012' 
Group By machine_name
Order by totalTicketCount DESC 

Your query will return you one row for each problem. Your first step is to group the result by machine so you get one row for each machine. You can then add a count column that shows you how many problems there were for that machine.

To find the maximum number of problems you need to put your query into a subselect so that you can extract the maximum. You can then use this as a subselect in a having clause to return the machines that have that maximum count.

SELECT machine_name, COUNT(machine_name) AS ticket_count
  FROM tickets
  WHERE date_submitted >= '01-DEC-2012' AND date_submitted <= '31-JAN-2012'
  GROUP BY machine_name
  HAVING ticket_count = (
    SELECT MAX(ticket_count) FROM (
      SELECT COUNT(machine_name) AS ticket_count
        FROM tickets
        WHERE date_submitted >= '01-DEC-2012' AND date_submitted <= '31-JAN-2012'
        GROUP BY machine_name
    )
  )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM