-1

I have this table.

+------+-------+--------+
| CODE | MONTH | AMOUNT |
+------+-------+--------+
|    2 |     1 |    100 |
|    3 |     1 |    200 |
|    2 |     2 |    300 |
|    3 |     2 |    400 |
+------+-------+--------+

And, this is the result that I'm trying to get.

+------+---------+---------+
| CODE |     MO1 |     MO2 |
+------+---------+---------+
|    2 |     100 |     300 |
|    3 |     200 |     400 |
+------+---------+---------+

I know that looping within select isn't possible.
I imagine I could maybe use CASE but I don't have a definite count of months as in my example above wherein there are only data for the months of January and February. I would have data for March later on. How can I do this?

Himanshu
  • 31,810
  • 31
  • 111
  • 133
esandrkwn
  • 399
  • 1
  • 6
  • 18

2 Answers2

5

Since you don't know the definite count of months you can do it dynamically like this:

SET @sql = NULL;
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'SUM(case when Month = ''',
      Month,
      ''' then Amount else 0 end) AS `Mo',
      Month, '`'
    )
  ) INTO @sql
FROM Table2;


SET @sql = CONCAT('SELECT Code, ', @sql, '
                  FROM Table2 
                  GROUP BY Code');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

Result:

| CODE | MO1 | MO2 |
--------------------
|    2 | 100 | 300 |
|    3 | 200 | 400 |

See this SQLFiddle

Himanshu
  • 31,810
  • 31
  • 111
  • 133
  • Thank you. I'm wondering though what those 3 single quotation marks mean. Phpmyadmin returns a syntax error near 'SELECT GROUP_CONCAT(DISTINCT CONCAT( 'SUM(case when Month = ''', '. – esandrkwn Apr 19 '13 at 07:18
  • @esandrkwn - Now see the updated answer. That was for `varchar` datatype while here month is not `varchar`. So removed it. – Himanshu Apr 19 '13 at 07:23
  • Thanks a lot I appreciate it very much. I'm not sure what's going on but I copied the codes exactly from fiddle after creating that same table Table2. When the delimiter is set as ';;' or without a delimiter I get the same error as above. When I set it as ';' I get this error: '#1243 - Unknown prepared statement handler (stmt) given to EXECUTE'. Any idea? – esandrkwn Apr 19 '13 at 07:53
  • @esandrkwn - Same issue is [here](http://stackoverflow.com/questions/12270953/combine-multiple-rows-into-one-mysql#comment-16458111). You need to do it with separate queries. One to build the `stmt` and one do execute it. [See the comment of the OP here](http://stackoverflow.com/questions/12270953/combine-multiple-rows-into-one-mysql#comment-16660514) – Himanshu Apr 19 '13 at 08:07
  • @esandrkwn - And please let me know once your do it successfully. – Himanshu Apr 19 '13 at 08:21
  • @esandrkwn - Hi I have edited my answer. Please check if it works for you now or not. – Himanshu Apr 30 '13 at 13:19
0
SELECT A.CODE AS CODE, A.AMOUNT as MO1, B.AMOUNT as MO2 FROM
((SELECT CODE, AMOUNT from Table1 WHERE MONTH=1) AS A
INNER JOIN
(SELECT CODE, AMOUNT from Table1 WHERE MONTH=2) AS B
ON
A.CODE=B.CODE)

Add more inner joins if more than 2 months

Hope this helps :)

faisal
  • 1,327
  • 10
  • 19