<small id='DfYwP'></small><noframes id='DfYwP'>

    1. <tfoot id='DfYwP'></tfoot>
    2. <legend id='DfYwP'><style id='DfYwP'><dir id='DfYwP'><q id='DfYwP'></q></dir></style></legend>
      <i id='DfYwP'><tr id='DfYwP'><dt id='DfYwP'><q id='DfYwP'><span id='DfYwP'><b id='DfYwP'><form id='DfYwP'><ins id='DfYwP'></ins><ul id='DfYwP'></ul><sub id='DfYwP'></sub></form><legend id='DfYwP'></legend><bdo id='DfYwP'><pre id='DfYwP'><center id='DfYwP'></center></pre></bdo></b><th id='DfYwP'></th></span></q></dt></tr></i><div id='DfYwP'><tfoot id='DfYwP'></tfoot><dl id='DfYwP'><fieldset id='DfYwP'></fieldset></dl></div>

        • <bdo id='DfYwP'></bdo><ul id='DfYwP'></ul>

        MySql 行入列、列入行

        时间:2023-06-03
        <tfoot id='Jvgmi'></tfoot>

            <tbody id='Jvgmi'></tbody>

          <small id='Jvgmi'></small><noframes id='Jvgmi'>

        • <legend id='Jvgmi'><style id='Jvgmi'><dir id='Jvgmi'><q id='Jvgmi'></q></dir></style></legend>

              <bdo id='Jvgmi'></bdo><ul id='Jvgmi'></ul>
                  <i id='Jvgmi'><tr id='Jvgmi'><dt id='Jvgmi'><q id='Jvgmi'><span id='Jvgmi'><b id='Jvgmi'><form id='Jvgmi'><ins id='Jvgmi'></ins><ul id='Jvgmi'></ul><sub id='Jvgmi'></sub></form><legend id='Jvgmi'></legend><bdo id='Jvgmi'><pre id='Jvgmi'><center id='Jvgmi'></center></pre></bdo></b><th id='Jvgmi'></th></span></q></dt></tr></i><div id='Jvgmi'><tfoot id='Jvgmi'></tfoot><dl id='Jvgmi'><fieldset id='Jvgmi'></fieldset></dl></div>
                1. 本文介绍了MySql 行入列、列入行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在将行转换为列以及将列转换为行时遇到问题.如果只是将行到列或列到行的转换,我可以做到这一点.

                  这是我的数据表

                  UNIT|JAN|FEB|MAR|APR|MEI|JUNCS-1|100|200|300|400|500|600CS-2|111|222|333|444|555|666CS-3|331|123|423|923|918|123

                  我想得到以下输出

                  MONTH|CS-1|CS-2|CS-3一月 |100 |111 |3312 月 |200 |222 |123三月 |300 |333 |423

                  等等.

                  有人知道怎么做吗?非常感谢!

                  解决方案

                  你可以这样做

                  选择月份,MAX(CASE WHEN unit = 'CS-1' THEN value END) `CS-1`,MAX(CASE WHEN unit = 'CS-2' THEN value END) `CS-2`,MAX(CASE WHEN unit = 'CS-3' THEN value END)`CS-3`从(选择单位、月、CASE月当 'JAN' 那么 jan当二月"然后二月当 'MAR' THEN MAR当四月"然后四月当可能"然后可能当'君'那么君结束值FROM table1 t CROSS JOIN(SELECT 'JAN' 月份 UNION ALL选择'FEB'联合所有选择'MAR'联合所有选择'APR'联合所有选择可以"联合所有选择君") C) q按月分组按字段排序(月,'JAN','FEB','MAR','APR','MAY','JUN')

                  输出:

                  <前>|月 |CS-1 |CS-2 |CS-3 ||-------|------|------|------||一月 |100 |111 |第331话|2 月 |200 |第222话123 ||三月 |300 |第333话第423话|四月|400 |第444话923 ||五月 |500 |第555话918 ||六月 |600 |第666话123 |

                  这是SQLFiddle 演示

                  i have problem with transposing row to column and column to row. I can do that if it just transpose row to column or column to row.

                  This my table with data

                  UNIT|JAN|FEB|MAR|APR|MEI|JUN
                  CS-1|100|200|300|400|500|600
                  CS-2|111|222|333|444|555|666
                  CS-3|331|123|423|923|918|123
                  

                  and I would like to get the following output

                  MONTH|CS-1|CS-2|CS-3
                  JAN  |100 |111 |331
                  FEB  |200 |222 |123
                  MAR  |300 |333 |423
                  

                  etc..

                  Anybody know how to do this? Thanks very much!

                  解决方案

                  You can do it this way

                  SELECT month,
                         MAX(CASE WHEN unit = 'CS-1' THEN value END) `CS-1`,
                         MAX(CASE WHEN unit = 'CS-2' THEN value END) `CS-2`,
                         MAX(CASE WHEN unit = 'CS-3' THEN value END) `CS-3`
                    FROM
                  (
                    SELECT unit, month,
                           CASE month 
                              WHEN 'JAN' THEN jan
                              WHEN 'FEB' THEN feb
                              WHEN 'MAR' THEN mar
                              WHEN 'APR' THEN apr
                              WHEN 'MAY' THEN may
                              WHEN 'JUN' THEN jun
                           END value
                      FROM table1 t CROSS JOIN
                    (
                      SELECT 'JAN' month UNION ALL
                      SELECT 'FEB' UNION ALL
                      SELECT 'MAR' UNION ALL
                      SELECT 'APR' UNION ALL
                      SELECT 'MAY' UNION ALL
                      SELECT 'JUN'
                    ) c
                  ) q
                   GROUP BY month
                   ORDER BY FIELD(month, 'JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN')
                  

                  Output:

                  | MONTH | CS-1 | CS-2 | CS-3 |
                  |-------|------|------|------|
                  |   JAN |  100 |  111 |  331 |
                  |   FEB |  200 |  222 |  123 |
                  |   MAR |  300 |  333 |  423 |
                  |   APR |  400 |  444 |  923 |
                  |   MAY |  500 |  555 |  918 |
                  |   JUN |  600 |  666 |  123 |
                  

                  Here is SQLFiddle demo

                  这篇关于MySql 行入列、列入行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 MySQL 中滞后列? 下一篇:我可以用纯 mysql 解决这个问题吗?(加入一列中的 ';' 分隔值)

                  相关文章

                2. <legend id='4SD1h'><style id='4SD1h'><dir id='4SD1h'><q id='4SD1h'></q></dir></style></legend>
                  <i id='4SD1h'><tr id='4SD1h'><dt id='4SD1h'><q id='4SD1h'><span id='4SD1h'><b id='4SD1h'><form id='4SD1h'><ins id='4SD1h'></ins><ul id='4SD1h'></ul><sub id='4SD1h'></sub></form><legend id='4SD1h'></legend><bdo id='4SD1h'><pre id='4SD1h'><center id='4SD1h'></center></pre></bdo></b><th id='4SD1h'></th></span></q></dt></tr></i><div id='4SD1h'><tfoot id='4SD1h'></tfoot><dl id='4SD1h'><fieldset id='4SD1h'></fieldset></dl></div>

                        <bdo id='4SD1h'></bdo><ul id='4SD1h'></ul>
                    1. <tfoot id='4SD1h'></tfoot>
                    2. <small id='4SD1h'></small><noframes id='4SD1h'>