SQL 编程帮助...使用 count 和 max ..等
我有一系列表格:
技术人员(pplSoft、fname、lname、pittID、专业知识、办公电话)
其中 fname 是名字,lname 是姓氏。用户(pplSoft、fname、lname、pittID、办公电话)
CATEGORIES(类别 ID、类别、描述)
其中此表列出了已提交工单的所有可能类别。INVENTORY(机器名称、IP、网络端口、MACADDR、位置 id)
LOCATIONS(位置 ID、位置、建筑物、注释)
TICKETS(票号、所有者 pplSoft、提交日期、关闭日期、工作天数、 类别 ID、机器名称、位置、描述)
ASSIGNMENT(票号、tech pplSoft、分配日期、状态)
其中持有的状态是枚举,可以是:已分配、进行中、委托、成功关闭或关闭不成功。
我的任务是 列出设备名称 具有最大数量的机器的所有名称 2011年12月和2012年1月这两个月出现了问题
。我必须把这个变成SQL。
我可以做这样的事情吗?
select machine_name
from tickets
where date_submitted >= '01-DEC-2012' and 'date_submitted <= '31-JAN-2012'
但我需要计算票数或使用 max 吗?
我如何从这里取得进步?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
与任何复杂的 SQL 查询一样,秘诀是将其分解为多个部分,最好是可独立测试的部分。
第一个问题是确定相关时期内每台机器拥有的票数。这里的标准是什么?也许,如果一台机器从 2011 年 11 月开始出现问题并持续到 2011 年 12 月,则应该计算在内;同样,如果问题于 2012 年 1 月开始但于 2012 年 2 月完成,则应计算在内。因此,我们需要:
如果您决定只考虑提交工单的日期,请将标准调整为引用
date_subscribed
两次;同样,如果仅计算工单完成的日期,则引用date_completed
两次。请注意,如果一台机器有一个 11 月开始的票证,直到 2 月才解决,上面的查询将对其计数;如果您使用其中任何一种替代方案,则该机器在相关期间没有出现任何问题。这告诉我们在该时间段内有多少张票可供机器使用。现在我们需要找出最大数量的票证:
现在我们需要选择具有此数量票证的机器名称:
假设 Oracle 支持WITH 子句,这可以被简化(相当大):
警告:我在子查询上使用了“AS 别名”,这是 SQL 标准所支持的。我相信 Oracle 不允许“AS 别名”,只需要在表名后加上“别名”;我不确定这是否也适用于子查询的名称。如果“AS m”和“AS n”符号引起问题,请尝试删除 AS。您可能会发现列重命名“AS num_tickets”等存在类似问题,但我相信 Oracle 确实允许在这种情况下使用 AS。
据推测,这只是一系列问题之一,因为答案似乎不需要除 Tickets 表之外的任何表。据推测,其他问题需要使用其他表格。
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:
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 referencedate_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:
Now we need to select the machine name(s) that had this number of tickets:
Assuming Oracle supports the WITH clause, this can be simplified (considerably):
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.
您需要使用
group by
。You need to use
group by
.您的查询将为每个问题返回一行。第一步是按机器对结果进行分组,以便每台机器获得一行。然后,您可以添加一个计数列,显示该机器存在多少问题。
要找到问题的最大数量,您需要将查询放入子选择中,以便可以提取最大值。然后,您可以将其用作having 子句中的子选择,以返回具有该最大计数的计算机。
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.