将行转换为mysql中的列

时间:2014-10-15 23:39:44

标签: mysql

我是mysql的新手,需要帮助将行转换为mysql数据库中的列 我已经介绍了一些转置的例子,但在使用正确的语法方面有些困惑 一些现有的答案增加了额外的要求,使其略有不同

我的退出表是行,并且行多于共享少数行以供解释

+----------+-----------+--------+----------+-----------+--------+
| Item     |     YEAR  |  Sales |   Profit |  TSV      |   REC  |
+----------+-----------+--------+----------+-----------+--------+
| machine  |      2011 |      10|     10   |   20      |     10 |
| machine  |      2010 |      8 |     15   |    6      |      2 |
| machine  |      2013 |      15|     5    |   11      |      5 |
| machine  |      2009 |      3 |     8    |    9      |     13 |
| machine  |      2012 |      6 |     12   |   18      |     16 |
| decors   |      2010 |      11|     10   |   13      |     22 |
| decors   |      2011 |      9 |     9    |   16      |     12 |
| decors   |      2012 |      3 |     7    |   11      |      9 |
| decors   |      2013 |      13|     23   |    9      |     10 |
| decors   |      2009 |      4 |     9    |   10      |      8 |
| sp parts |      2009 |      22|     25   |   17      |     22 |
| sp parts |      2010 |      21|     22   |   11      |     14 |
| sp parts |      2011 |      28|     30   |   29      |     23 |
| sp parts |      2012 |      23|     28   |   12      |      8 |
| sp parts |      2013 |      21|     24   |   24      |     32 |
-----------------------------------------------------------------
转置后的

和预期表

------------------------------------------------------------------------------------------------------------------------------------------
 | Item         machine machine machine machine machine decors  decors  decors  decors decors sp parts sp parts sp parts sp parts sp parts |
  -----------------------------------------------------------------------------------------------------------------------------------------|
 | YEAR     2011    2010    2013    2009    2012    2010    2011    2012    2013        2009    2009    2010    2011    2012    2013       |
 | Sales    10  8   15  3   6   11  9   3   13  4   22  21  28  23  21         |
 | Profit   10  15  5   8   12  10  9   7   23  9   25  22  30  28  24         |
 | TSV          20  6   11  9   18  13  16  11  9   10  17  11  29  12  24         |
 | REC      10  2   5   13  16  22  12  9   10  8   22  14  23  8   32         |
  ------------------------------------------------------------------------------------------------------------------------------------------

有人可以帮助解决这个问题

0 个答案:

没有答案