1

Sorry for an unclear question, my english isnt that good. So theres my query:

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

and this is the result:

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

My goal was to count ticket by their status, and if status='open' and assign = null then the status would change to 'unassigned', i need a better solution or just a way to merge the result where 'James' and 'Nick' to be one, as i only need to know whether the ticket is assigned or not.

3 Answers 3

1

Don't see where you need department table, think you can remove it safely, but...

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 (simplified)

Sign up to request clarification or add additional context in comments.

6 Comments

Excellent! Can you improve it abit so that it can display other status aswell? i.e status closed count = 0
Oh and i also need the ticketID.
@user1918956 well, edited with ticketID (assuming one ticketID can be present in many rows). But I'm not sure that this will still give you what you want. For the other status (statusName) they should be already displayed (overdue in your sample for example). But if they don't, could you give a "bigger" sample ?
sorry i meant i want the statusID, and heres a part of my ticket table : puu.sh/1Sqaq And here my status table : puu.sh/1SqcX where status in ticket is a foreign key of statusID.
Nevermine i figured that out of your example, thanks alot :D. Btw i still want other status (solve or closed) even if they arent on the ticket table. i.e closed = 0.
|
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

This query fetches the unassigned and assigned tickets in the form of a union of two disjoint resultsets.

Comments

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

2 Comments

i think you missed this one, then the status would change to 'unassigned'
I am thinking that the OP was referring to how things are counted, not actual status change updates, maybe I am wrong though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.