查询 - 查找号码。从没有关闭的门票。已创建的门票

时间:2016-08-16 07:13:33

标签: mysql sql subquery

我有一个查询,查找每个队列(部门)的代理创建的票证数量。

SELECT queue.name AS 'Queue', COUNT(ticket.id) AS '# of tickets'
FROM ticket
    INNER JOIN
        queue ON ticket.queue_id = queue.id
WHERE
    ticket.create_by != 1
AND
    DATE(ticket.create_time) >= DATE_SUB(CURDATE(),INTERVAL 1 day)
AND
    ticket.queue_id IN ( SELECT id FROM queue WHERE valid_id=1 ORDER BY name)
GROUP BY queue.name;

我想要的是从创建的门票中获取多少张门票。这是上述查询结果的结构:

Queue         ||      # of tickets


Billing                12
Enterprise Support     7
Fiber Support          47
L2-Support             4
Retail Support         107
Sales                  4

我在子查询中不太好。这就是为什么我在这个查询中挣扎。 以下查询显示处于关闭状态的票证数

    SELECT queue.name AS 'Queue', COUNT(ticket.id) AS '# of closed tickets'
    FROM ticket
        INNER JOIN
            queue ON ticket.queue_id = queue.id
    WHERE
        ticket.create_by != 1
    AND
ticket.ticket_state_id = '2'
    AND
        DATE(ticket.create_time) >= DATE_SUB(CURDATE(),INTERVAL 1 day)
    AND
        ticket.queue_id IN ( SELECT id FROM queue WHERE valid_id=1 ORDER BY name)
    GROUP BY queue.name;

最终我想要显示队列创建的票数以及有多少票证处于关闭状态,如下所示:

Queue         ||      # of tickets       || # of closed tickets



Billing                12                    2
Enterprise Support     7                     0
Fiber Support          47                     22
L2-Support             4                      1 
Retail Support         107                     60
Sales                  4                       1

0 个答案:

没有答案