我有一个需要帮助的简单 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
希望这是有道理的。
谢谢