I have a dataset in mysql that contains the results of bandwidth tests for multiple named interfaces, with the date the test was performed and the name of the interface. A sample dataset would look something like this:
| date | testResult | interface |
---------------------------------------
| 12/25 | 32 | eth0 |
| 12/25 | 21 | eth0 |
| 12/25 | 25 | eth0 |
| 12/26 | 30 | eth0 |
| 12/27 | 33 | eth0 |
| 12/25 | 12 | eth1 |
| 12/25 | 16 | eth1 |
| 12/27 | 3 | vz0 |
| 12/26 | 120 | virt1 |
I need to chart the average result for each interface on a given day, so my current solution is
SELECT `date`, AVG(`testResult`) as avg, `interface` FROM `tests` WHERE 1=1
GROUP BY date, interface ORDER BY interface, date
This gives me a result like
| date | avg | interface |
---------------------------------------
| 12/25 | 26 | eth0 |
| 12/26 | 30 | eth0 |
| 12/27 | 33 | eth0 |
| 12/25 | 14 | eth1 |
| 12/26 | 120 | virt1 |
| 12/27 | 3 | vz0 |
My problem is that I need this data "joined" (I think) on the date, with a column for each interface's average on that day. The name and number of interfaces in a result set is not constant and can't be hardcoded. My ideal result set would look like this:
| date | avg_eth0 | avg_eth1 | avg_virt1 | avg_vz0 |
-------------------------------------------------------------------
| 12/25 | 26 | 14 | NULL | NULL |
| 12/26 | 30 | NULL | 120 | NULL |
| 12/27 | 33 | NULL | NULL | 3 |
Is there a way to perform this join, basically creating a column for each unique value of the interface
column?