0

我有一个需要帮助的简单 SQL 命令。

SELECT "01 Tickets within SLA in January: " as "Category",count(tickets.id) as "Total"     FROM tickets
  WHERE (created_at>=date('2013-01-01 00:00:00')) AND (created_at<=date('2013-01-31     23:59:00')) AND (closed_at<due_at) 
union
 SELECT "02 Tickets within SLA in February: " as "Category",count(tickets.id) as "Total"     from tickets
  WHERE (created_at>=date('2013-02-01 00:00:00')) AND (created_at<=date('2013-02-31     23:59:00')) AND (closed_at<due_at)
union
  SELECT "03 Tickets within SLA in March: " as "Category",count(tickets.id) as "Total"     from tickets
  WHERE (created_at>=date('2013-03-01 00:00:00')) AND (created_at<=date('2013-03-31     23:59:00')) AND (closed_at<due_at)
union
  SELECT "04 Tickets within SLA in April: " as "Category",count(tickets.id) as "Total"     from tickets
  WHERE (created_at>=date('2013-04-01 00:00:00')) AND (created_at<=date('2013-04-31     23:59:00')) AND (closed_at<due_at)
union
  SELECT "05 Tickets within SLA in May: " as "Category",count(tickets.id) as "Total" from     tickets
  WHERE (created_at>=date('2013-05-01 00:00:00')) AND (created_at<=date('2013-05-31     23:59:00')) AND (closed_at<due_at)
union
 SELECT "06 Tickets within SLA in June: " as "Category",count(tickets.id) as "Total" from     tickets
  WHERE (created_at>=date('2013-06-01 00:00:00')) AND (created_at<=date('2013-06-31     23:59:00')) AND (closed_at<due_at)
union
  SELECT "07 Tickets within SLA in July: " as "Category",count(tickets.id) as "Total" from     tickets
  WHERE (created_at>=date('2013-07-01 00:00:00')) AND (created_at<=date('2013-07-31    23:59:00')) AND (closed_at<due_at) 

 ORDER by Category

此查询为每个查询返回一个值,这很好。但是,我想在应用“AND (closed_at) 之前返回 count(tickets.id)

所以它会返回这样的东西。

Category                          Total  Total Tickets
01 Tickets within SLA in January:   131     400
02 Tickets within SLA in February:  132     500
03 Tickets within SLA in March:      87     100
04 Tickets within SLA in April:     121      99

希望这是有道理的。

谢谢

4

3 回答 3

3

不要尝试在 SQL 中格式化您的结果 - 这应该在您的表示层中完成。您可以通过简单地按日期的年和月进行分组来使查询更简单,然后使用此信息来生成结果:

SELECT YEAR(date), MONTH(date), count(tickets.id), SUM(CASE WHEN closed_at < due_at THEN 1 ELSE 0 END) as matching
from     tickets
GROUP BY YEAR(date), MONTH(date)
ORDER BY category
于 2013-04-29T22:56:45.113 回答
0

以下是您如何计算符合更具体条件的门票,同时计算总门票:

 SELECT "01 Tickets within SLA in January: " as "Category",
        SUM(closed_at<due_at) as "Total",
        COUNT(tickets.id) as "Total Tickets"
 FROM tickets
 WHERE created_at BETWEEN '2013-01-01 00:00:00' AND '2013-01-31 23:59:00'

您可以每个月重复此操作,或按照其他地方的建议使用分组。

于 2013-04-29T22:59:52.670 回答
0
SELECT CONCAT(MONTH(created_at), ' Tickets within SLA in ', MONTHNAME(created_at)) `Category`
      ,SUM(closed_at<due_at) `Total`
      ,count(tickets.id) `Total Tickets`
  FROM tickets
 where created_at>=date('2013-01-01') AND created_at<date('2013-08-01')
 GROUP BY MONTH(created_at), MONTHNAME(created_at)
 ORDER by `Category`;

注意:为了精确和理智,最好使用表格

WHERE ADATE >= {start} AND ADATE < {next-start}

这在数学意义上是绝对明确的,并且在{next-start}之前没有留下任何纳秒。

于 2013-04-29T23:03:07.280 回答