Wednesday, 11 September 2013

Can I build semi-advanced reports using only MySQL queries?

Can I build semi-advanced reports using only MySQL queries?

I have a MySQL table whose structure and data looks something like this:
name | date | type | value
-----+------------+-------+------
foo | 2013-01-01 | blue | 4
foo | 2013-01-02 | green | 1
foo | 2013-01-01 | blue | 9
foo | 2013-01-02 | green | 5
bar | 2013-01-01 | blue | 10
bar | 2013-01-02 | green | 4
bar | 2013-01-01 | blue | 6
bar | 2013-01-02 | green | 2
meow | 2013-01-01 | blue | 5
meow | 2013-01-02 | green | 6
meow | 2013-01-01 | blue | 4
meow | 2013-01-02 | green | 4
I'm trying to construct a query that will produce this output:
name | blue | green
-----+------+------
foo | 13 | 6
bar | 16 | 6
meow | 9 | 10
The name column is maintained. The blue and green columns of the output
are generated based on the values of the type table column. The values of
those two columns are the accumulated value of the value table column for
all dates. I hope that made sense.
This is a bit out of my league when it comes to MySQL so I have no idea
where to start. Is this possible using MySQL, and if so, how? For
instance, how does one create output columns based on the values of a
single table column?

No comments:

Post a Comment