I have two tables. The first table has a column with a lot of different values that I want to break up into several columns when showing the result. Then I would like to combine these results with table two:
Notice that table one has hourly values while table two only has dates whith time equal to 00:00:00.
Table 1
+---------------------+------+----+
| DateAndTime | Area | V1 |
+---------------------+------+----+
| 2012-01-01 00:00:00 | A1 | 3 |
| 2012-01-01 00:01:00 | A1 | 4 |
| 2012-01-01 00:00:00 | A2 | 4 |
| 2012-01-01 00:01:00 | A2 | 2 |
+---------------------+------+----+
I would like to split the area columns into several columns, and thereafter combine with table 2 based on date.
Table 2
+---------------------+----+
| DateAndTime | V2 |
+---------------------+----+
| 2012-01-01 00:00:00 | 3 |
| 2012-01-02 00:00:00 | 4 |
+---------------------+----+
I would like to have the end result to be printed like this:
+---------------------+----+----+----+
| DateAndTime | A1 | A2 | V2 |
+---------------------+----+----+----+
| 2012-01-01 00:00:00 | 3 | 4 | 3 |
| 2012-01-01 00:01:00 | 4 | 2 | 4 |
+---------------------+----+----+----+
There can be several area values (A1, A2, A3, …) and the data above is simplified to make my point 🙂
I hope I am not double posting and that someone can help me.
What you are trying to achieve here is a transposition of the different values of Area.
It’s decently possible with a fixed and limited number of them but if this number is not bounded then you simply can’t do it in MySQL and will have to rely on your application layer to do it 🙁
Drop a comment if you wan’t a theorical example on how it is possible 🙂