<bdo id='vS9To'></bdo><ul id='vS9To'></ul>
        <legend id='vS9To'><style id='vS9To'><dir id='vS9To'><q id='vS9To'></q></dir></style></legend>

      1. <tfoot id='vS9To'></tfoot>
      2. <small id='vS9To'></small><noframes id='vS9To'>

        <i id='vS9To'><tr id='vS9To'><dt id='vS9To'><q id='vS9To'><span id='vS9To'><b id='vS9To'><form id='vS9To'><ins id='vS9To'></ins><ul id='vS9To'></ul><sub id='vS9To'></sub></form><legend id='vS9To'></legend><bdo id='vS9To'><pre id='vS9To'><center id='vS9To'></center></pre></bdo></b><th id='vS9To'></th></span></q></dt></tr></i><div id='vS9To'><tfoot id='vS9To'></tfoot><dl id='vS9To'><fieldset id='vS9To'></fieldset></dl></div>

        Mysql 将列转换为行(数据透视表)

        Mysql Convert Column to row (Pivot table )(Mysql 将列转换为行(数据透视表))
        <legend id='RCoys'><style id='RCoys'><dir id='RCoys'><q id='RCoys'></q></dir></style></legend>

          <bdo id='RCoys'></bdo><ul id='RCoys'></ul>

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

                <tbody id='RCoys'></tbody>

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

              2. <tfoot id='RCoys'></tfoot>

                1. 本文介绍了Mysql 将列转换为行(数据透视表)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我有一张这样的桌子

                  +---+-----+----+----+----+----+|id |月|col1|col2|col3|col4|+---+-----+----+----+----+----+|101|一月 |A |B |NULL|B |+---+-----+----+----+----+----+|102|feb |C |A |G |E |+---+-----+----+----+----+----+

                  然后我想创建这样的报告

                  +----+---+---+|desc|一月|二月|+----+---+---+|col1|A |C |+----+---+---+|col2|B |A |+----+---+---+|col3|0 |G |+----+---+---+|Col4|B |E |+----+---+---+

                  有人可以帮忙吗?

                  解决方案

                  您需要做的是首先对数据进行反透视,然后对其进行透视.但不幸的是,MySQL 没有这些函数,因此您需要使用 UNION ALL 查询来复制它们以进行反透视,并使用带有 CASE 的聚合函数作为透视.>

                  unpivot 或 UNION ALL 片段从 col1、col2 等中获取数据并将其转换为多行:

                  select id, month, col1 value, 'col1' 描述从你的桌子联合所有选择 id、月份、col2 值、'col2' 描述从你的桌子联合所有选择 ID、月份、col3 值、col3"描述从你的桌子联合所有选择 id、月份、col4 值、'col4' 描述从你的桌子

                  参见SQL Fiddle with Demo.

                  结果:

                  <代码>|身份证 |月 |价值 |描述 |----------------------------------|101 |简 |一个 |列 1 ||102 |二月 |C |列 1 ||101 |简 |乙 |col2 ||102 |二月 |一个 |col2 ||101 |简 |(空) |col3 ||102 |二月 |G |col3 ||101 |简 |乙 |col4 ||102 |二月 |E |col4 |

                  然后将其包装在子查询中以应用聚合和 CASE 将其转换为您想要的格式:

                  选择描述,max(case when month = 'jan' then value else 0 end) jan,max(case when month = 'feb' then value else 0 end) feb从(选择 id、月份、col1 值、'col1' 描述从你的桌子联合所有选择 id、月份、col2 值、'col2' 描述从你的桌子联合所有选择 ID、月份、col3 值、col3"描述从你的桌子联合所有选择 id、月份、col4 值、'col4' 描述从你的桌子) 源文件按描述分组

                  参见SQL Fiddle with demo

                  结果是:

                  <代码>|描述 |一月 |2 月 |-----------------------|列 1 |一个 |C ||col2 |乙 |一个 ||col3 |0 |G ||col4 |乙 |E |

                  I have a table like this

                  +---+-----+----+----+----+----+
                  |id |month|col1|col2|col3|col4|
                  +---+-----+----+----+----+----+
                  |101|Jan  |A   |B   |NULL|B   |
                  +---+-----+----+----+----+----+
                  |102|feb  |C   |A   |G   |E   |
                  +---+-----+----+----+----+----+
                  

                  And then I want to create report like this

                  +----+---+---+
                  |desc|jan|feb|
                  +----+---+---+
                  |col1|A  |C  |
                  +----+---+---+
                  |col2|B  |A  |
                  +----+---+---+
                  |col3|0  |G  |
                  +----+---+---+
                  |Col4|B  |E  |
                  +----+---+---+
                  

                  Can anyone help with this?

                  解决方案

                  What you need to do is first, unpivot the data and then pivot it. But unfortunately MySQL does not have these functions so you will need to replicate them using a UNION ALL query for the unpivot and an aggregate function with a CASE for the pivot.

                  The unpivot or UNION ALL piece takes the data from your col1, col2, etc and turns it into multiple rows:

                  select id, month, col1 value, 'col1' descrip
                  from yourtable
                  union all
                  select id, month, col2 value, 'col2' descrip
                  from yourtable
                  union all
                  select id, month, col3 value, 'col3' descrip
                  from yourtable
                  union all
                  select id, month, col4 value, 'col4' descrip
                  from yourtable
                  

                  See SQL Fiddle with Demo.

                  Result:

                  |  ID | MONTH |  VALUE | DESCRIP |
                  ----------------------------------
                  | 101 |   Jan |      A |    col1 |
                  | 102 |   feb |      C |    col1 |
                  | 101 |   Jan |      B |    col2 |
                  | 102 |   feb |      A |    col2 |
                  | 101 |   Jan | (null) |    col3 |
                  | 102 |   feb |      G |    col3 |
                  | 101 |   Jan |      B |    col4 |
                  | 102 |   feb |      E |    col4 |
                  

                  You then wrap this in a subquery to apply the aggregate and the CASE to convert this into the format you want:

                  select descrip, 
                    max(case when month = 'jan' then value else 0 end) jan,
                    max(case when month = 'feb' then value else 0 end) feb
                  from
                  (
                    select id, month, col1 value, 'col1' descrip
                    from yourtable
                    union all
                    select id, month, col2 value, 'col2' descrip
                    from yourtable
                    union all
                    select id, month, col3 value, 'col3' descrip
                    from yourtable
                    union all
                    select id, month, col4 value, 'col4' descrip
                    from yourtable
                  ) src
                  group by descrip
                  

                  See SQL Fiddle with demo

                  The result is:

                  | DESCRIP | JAN | FEB |
                  -----------------------
                  |    col1 |   A |   C |
                  |    col2 |   B |   A |
                  |    col3 |   0 |   G |
                  |    col4 |   B |   E |
                  

                  这篇关于Mysql 将列转换为行(数据透视表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
                  SQL query to group by day(按天分组的 SQL 查询)
                  What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
                  MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
                  MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
                  Include missing months in Group By query(在 Group By 查询中包含缺失的月份)

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

                        <bdo id='JdnRK'></bdo><ul id='JdnRK'></ul>
                        <i id='JdnRK'><tr id='JdnRK'><dt id='JdnRK'><q id='JdnRK'><span id='JdnRK'><b id='JdnRK'><form id='JdnRK'><ins id='JdnRK'></ins><ul id='JdnRK'></ul><sub id='JdnRK'></sub></form><legend id='JdnRK'></legend><bdo id='JdnRK'><pre id='JdnRK'><center id='JdnRK'></center></pre></bdo></b><th id='JdnRK'></th></span></q></dt></tr></i><div id='JdnRK'><tfoot id='JdnRK'></tfoot><dl id='JdnRK'><fieldset id='JdnRK'></fieldset></dl></div>
                      • <tfoot id='JdnRK'></tfoot>

                              <tbody id='JdnRK'></tbody>
                            <legend id='JdnRK'><style id='JdnRK'><dir id='JdnRK'><q id='JdnRK'></q></dir></style></legend>