对于一个不明确的问题抱歉,我的英语不太好。所以我的查询是:
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 '和'尼克'是一个,因为我只需要知道票是否被分配。
答案 0 :(得分: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(简化)
答案 1 :(得分: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
此查询以两个不相交的结果集的并集形式提取未分配和已分配的票证。
答案 2 :(得分: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