1

对不起一个不清楚的问题,我的英语不是那么好。所以有我的查询:

SELECT ticketID, 
status,
COUNT(status) as count,
statusName,
assign   
FROM ticket, department, status 
WHERE ticket.department = 100 
AND ticket.department = department.departID 
AND ticket.status = status.statusID
GROUP BY statusName,assign

这是结果:

| ticketID   | count       | statusName | assign |
|:----------|-----------:|:---------:|:-------:
| 1002       |           2 |       open |   NULL |
| 1020       |           1 |       open |  James |
| 1021       |           1 |       open |   Nick |
| 1015       |           1 |    overdue |   NULL |

我的目标是按状态计算票证,如果 status='open' 和 assign = null 则状态将更改为 'unassigned',我需要一个更好的解决方案,或者只是一种合并结果的方法,其中 'James' 和 ' Nick' 成为其中之一,因为我只需要知道票是否已分配。

4

3 回答 3

1

看不到你需要部门表的地方,认为你可以安全地删除它,但是......

SELECT a.statusID, a.Status, Count(*) 
FROM(
   SELECT statusID,
   (CASE WHEN statusName = 'open' and assign IS NULL THEN 'unassigned' 
         WHEN statusName ='open' and assign IS NOT NULL THEN 'assigned'
         ELSE statusName
         END) as Status
   FROM ticket 
   INNER JOIN department ON ticket.department = department.departID 
   INNER JOIN status ON ticket.status = status.statusID
   WHERE ticket.department = 100) as a
GROUP BY a.Status, a.statusID

SqlFiddle(简体)

于 2013-01-24T15:50:50.763 回答
1
SELECT ticketID, 
status,
COUNT(status) as count,
statusName,
'unassigned'   
FROM ticket, department, status 
WHERE ticket.department = 100 
AND ticket.department = department.departID 
AND ticket.status = status.statusID
AND assign is NULL
GROUP BY statusName
UNION
SELECT ticketID, 
status,
COUNT(status) as count,
statusName,
'assigned'   
FROM ticket, department, status 
WHERE ticket.department = 100 
AND ticket.department = department.departID 
AND ticket.status = status.statusID
AND assign is NOT NULL
GROUP BY statusName

此查询以两个不相交的结果集的并集形式获取未分配和已分配的票证。

于 2013-01-24T16:34:42.730 回答
0
SELECT ticketID,status,sum(CASE status 
    WHEN 'open' then 1
    WHEN '' then 1
    WHEN NULL then 1
    else 0) as count,statusName,assign   
        FROM ticket, department, status WHERE ticket.department = 100 
        AND ticket.department = department.departID 
        AND ticket.status = status.statusID
        GROUP BY statusName,assign
于 2013-01-24T15:37:15.843 回答