I have a call logging database that I would like to pull team information from. It is running on an old MySQL 4.0.16 server so I am struggling to get the data without subqueries/views and am looking for a push in the right direction as I am still quite new to MySQL/PHP.
The source table is in the form of:
callid | team | status
---------------------------
15432 | team A | open
15433 | team B | open
15434 | team A | onhold
15435 | team A | closed
15436 | team A | closed
and I would like to output the number of calls per team per status so it should look like:
team | open | onhold | closed
--------------------------------
team A | 1 | 1 | 2
team B | 1 | 0 | 0
This can either be in a select statement or a using a temp table and multiple queries as either of these will allow me to use the data to display overall team information.
I’ve created temp table and filled it with the team names and zeros for the number of onhold etc but am then failing to come up with an update statement that will then add the number calls per status per team without a subquery. What is the best way of going about this in MySQL 4.0.16? Thank you for any help you can provide.
You can use an aggregate function with a case expression to get the data in the format that you want:
See SQL Fiddle with Demo
Result is: