简体   繁体   English

SQL编程帮助…使用count和max ..etc

[英]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. TECH PERSONNEL (pplSoft, fname, lname, pittID, expertise, office phone) ,其中fname是名字,lname是姓氏。

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

  • CATEGORIES (category id, category, description) where this table lists all possible categories of submitted tickets. CATEGORIES (category id, category, description) ,其中此表列出了提交的票证的所有可能类别。

  • 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. ASSIGNMENT (ticket number, tech pplSoft, date assigned, status) ,其中保留的状态是枚举,可以是:分配,进行中,委派,成功关闭或不成功关闭。

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. 我的任务是列出“设备名称”,列出2011年12月和2012年1月两个月中出现问题最多的计算机的所有名称。

I have to turn this into SQL. 我必须将其转换为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. 与任何复杂的SQL查询一样,秘密在于将其分解为多个部分,最好是可独立测试的部分。

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; 如果一台机器的问题从2011年11月开始并一直持续到2011年12月,则应该计算在内; likewise, if a problem was started in January 2012 but completed in February 2012, that should be counted. 同样,如果问题在2012年1月开始但在2012年2月完成,则应计算在内。 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; 如果您决定仅是票证提交的日期,请调整标准两次以引用date_submitted likewise, if it is only the dates when the tickets were completed that count, then reference date_completed twice. 同样,如果仅凭票完成的日期进行计数,则引用date_completed两次。 Note that if a machine had a ticket that was started in November and not resolved until February, the query above will count it; 请注意,如果一台计算机的票证是从11月开始的,直到2月才解决,则上面的查询将对其进行计数; 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): 假设Oracle支持WITH子句,则可以简化(相当大):

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. 警告 :如SQL标准所支持,我在子查询上使用了“ AS别名”。 I believe Oracle does not allow 'AS alias' and requires just 'alias' after table names; 我相信Oracle不允许使用“ AS别名”,而在表名后只需要“别名”即可。 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. 如果“ AS m”和“ AS n”符号引起麻烦,请尝试删除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. 您可能会发现列重命名为“ AS num_tickets”等类似的问题,但是我相信Oracle在这种情况下确实允许使用AS。

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 . 您需要使用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. 然后,您可以将其用作Have子句中的子选择,以返回具有该最大计数的计算机。

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
    )
  )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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