0

MySQL

I have the following query

"SELECT Status, COUNT( ticket_id) AS total FROM tickets GROUP BY Status"

Which returns

Status ¦ Total  
0      ¦   3
2      ¦   1
3      ¦   6

Is it possible to change the result to a pharse, for example if

0 = Open, 1 = Hold, 2 = Awaiting End User Response, 3 = Resolved, 4 = Closed.

So then the result of the query would be

Status                 ¦ Total  
Open                   ¦   3
Awaiting end user      ¦   1
Resolved               ¦   6

Any help would be awesome, Cheers!

1

1 Answer 1

3

You need case expression :

select (case (Status) 
               when 0 then 'Open' 
               when 1 then 'Hold'
               when 2 then 'Awaiting End User Response'
               when 3 then 'Resolved'
               when 4 then 'Closed'
        end) as total, count(ticket_id) AS total 
from tickets t
group by (case (Status) 
               when 0 then 'Open' 
               when 1 then 'Hold'
               when 2 then 'Awaiting End User Response'
               when 3 then 'Resolved'
               when 4 then 'Closed'
          end);
Sign up to request clarification or add additional context in comments.

3 Comments

As Yogesh stated, you can use a case statement to replace the status integer with the phrase. Alternatively, you could create a table giving the relationship between the code and the description for the status, join to that table, and display the description instead of the code. The code tables ( Status_Code, Status_Description in this case) are really useful for ease of maintenance.
@RiggsFolly thank you for your response. I moved my commentary.
@yogeshsharma Thanks for your assisstance it is working exactly as I need. Top Man!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.