我的任务是生成一些关于我们的请求跟踪器使用情况的报告。Request Tracker是我工作的几个部门使用的票务系统。为此,我每晚将有关当天更改的门票的详细信息拍摄到另一个数据库中。这种方法将我的报告与 RT 使用的内部数据库模式分离。
在报告的许多其他问题中,我需要报告每个部门每个月解决了多少票。在 RT 中,部门存储为 CustomField,我的建模遵循这种趋势,正如您在下面的查询中看到的那样。但是,由于我每天晚上抓取快照的方式,一张票有多行,并且部门字段可能会在一个月内发生变化。我只对最近的部门领域感兴趣。我不知道如何在查询中得到它。
我知道我可以使用“GROUP BY”将查询结果减少到每张票一个,但是当我这样做时,我不知道如何获取最后一个部门设置。由于部门都是字符串,因此 MAX() 不会得到最后一个。MySQL 不要求您对正在选择的字段使用聚合函数,但结果是不确定的(从我的测试看来,它可能会抢占我的 MySQL 版本中的第一个)。
为了说明,这里是一个查询的结果,它显示了两张票,以及所有它的部门字段设置:
"ticket_num","date","QueueName","CF","CFValue","closed"
35750,"2009-09-22","IT_help","Department","",""
35750,"2009-09-23","IT_help","Department","",""
35750,"2009-09-24","IT_help","Department","",""
35750,"2009-09-25","IT_help","Department","",""
35750,"2009-09-26","IT_help","Department","",""
35750,"2009-10-02","IT_help","Department","",""
35750,"2009-10-03","IT_help","Department","",""
35750,"2009-10-12","IT_help","Department","",""
35750,"2009-10-13","IT_help","Department","",""
35750,"2009-10-26","IT_help","Department","Conference/Visitors","2009-10-26 10:10:32"
35750,"2009-10-27","IT_help","Department","Conference/Visitors","2009-10-26 10:10:32"
36354,"2009-10-20","IT_help","Department","",""
36354,"2009-10-21","IT_help","Department","",""
36354,"2009-10-22","IT_help","Department","FS Students",""
36354,"2009-10-23","IT_help","Department","FS Students",""
36354,"2009-10-26","IT_help","Department","FS Students","2009-10-26 12:23:00"
36354,"2009-10-27","IT_help","Department","FS Students","2009-10-26 12:23:00"
如我们所见,两张票都在 26 日关闭,并且两张票在首次出现时的几天内都有空的 Department 字段。我在下面包含了我的查询,您可以看到我人为地限制了 where 语句后半部分返回的列数:
SELECT d.ticket_num, d.date, q.name as QueueName, cf.name as CF, cfv.value as CFValue, d.closed
FROM daysCF dcf
INNER JOIN daily_snapshots d on dcf.day_id = d.id
INNER JOIN Queues q on d.queue_id = q.id
INNER JOIN CustomFieldValues cfv on dcf.cfv_id = cfv.id
INNER JOIN CustomFields cf on cf.id = cfv.field_id
WHERE cf.name = 'Department' and (d.ticket_num = 35750 or d.ticket_num = 36354)
ORDER by d.ticket_num, d.date
如何修改该查询,以便我得到一个结果集,告诉我在 10 月份有一张为“FS 学生”关闭的票和一张为“会议/访客”关闭的票?