<legend id='Rh5YE'><style id='Rh5YE'><dir id='Rh5YE'><q id='Rh5YE'></q></dir></style></legend>

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

    1. <small id='Rh5YE'></small><noframes id='Rh5YE'>

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

      分解表以按列进行透视(SQL、PYSPARK)

      时间:2023-08-22

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

    4. <tfoot id='e13Is'></tfoot>

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

            <bdo id='e13Is'></bdo><ul id='e13Is'></ul>
              1. 本文介绍了分解表以按列进行透视(SQL、PYSPARK)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在使用 AWS Glue 中的 python3.6 环境在 pyspark 中工作.我有这张桌子:

                I'm working in an environment pyspark with python3.6 in AWS Glue. I have this table :

                +----+-----+-----+-----+
                |year|month|total| loop|
                +----+-----+-----+-----+
                |2012|    1|   20|loop1|
                |2012|    2|   30|loop1|
                |2012|    1|   10|loop2|
                |2012|    2|    5|loop2|
                |2012|    1|   50|loop3|
                |2012|    2|   60|loop3|
                +----+-----+-----+-----+
                

                我需要得到如下输出:

                year    month   total_loop1 total_loop2 total_loop3
                2012    1         20           10           50
                2012    2         30           5            60
                

                我越接近 SQL 代码:

                The closer I have gotten is with the SQL code:

                select a.year,a.month, a.total,b.total from test a 
                left join test b
                on a.loop <> b.loop 
                and a.year = b.year and a.month=b.month
                

                输出仍然到目前为止:

                +----+-----+-----+-----+
                |year|month|total|total|
                +----+-----+-----+-----+
                |2012|    1|   20|   10|
                |2012|    1|   20|   50|
                |2012|    1|   10|   20|
                |2012|    1|   10|   50|
                |2012|    1|   50|   20|
                |2012|    1|   50|   10|
                |2012|    2|   30|    5|
                |2012|    2|   30|   60|
                |2012|    2|    5|   30|
                |2012|    2|    5|   60|
                |2012|    2|   60|   30|
                |2012|    2|   60|    5|
                +----+-----+-----+-----+
                

                我该怎么做?非常感谢

                推荐答案

                表脚本和示例数据

                CREATE TABLE [TableName](
                    [year] [nvarchar](50) NULL,
                    [month] [int] NULL,
                    [total] [int] NULL,
                    [loop] [nvarchar](50) NULL
                ) 
                
                INSERT [TableName] ([year], [month], [total], [loop]) VALUES (N'2012', 1, 20, N'loop1')
                INSERT [TableName] ([year], [month], [total], [loop]) VALUES (N'2012', 2, 30, N'loop1')
                INSERT [TableName] ([year], [month], [total], [loop]) VALUES (N'2012', 1, 10, N'loop2')
                INSERT [TableName] ([year], [month], [total], [loop]) VALUES (N'2012', 2, 5, N'loop2')
                INSERT [TableName] ([year], [month], [total], [loop]) VALUES (N'2012', 1, 50, N'loop3')
                INSERT [TableName] ([year], [month], [total], [loop]) VALUES (N'2012', 2, 60, N'loop3')
                

                使用枢轴功能...

                SELECT * 
                FROM   TableName
                       PIVOT(Max([total]) 
                            FOR [loop] IN ([loop1], [loop2], [loop3]) ) pvt
                

                在线演示:http://www.sqlfiddle.com/#!18/164a4/1/0

                如果您正在寻找动态解决方案,那么试试这个...(动态枢轴)

                If you are looking for a dynamic solution, then try this... (Dynamic Pivot)

                DECLARE @cols AS NVARCHAR(max) = Stuff((SELECT DISTINCT ',' + Quotename([loop])
                         FROM   TableName
                         FOR xml path(''), type).value('.', 'NVARCHAR(MAX)'), 1, 1, ''); 
                
                DECLARE @query AS NVARCHAR(max) =  'SELECT * 
                                                    FROM   TableName
                                                           PIVOT(Max([total]) 
                                                                FOR [loop] IN ('+ @cols +') ) pvt';
                
                EXECUTE(@query) 
                

                在线演示:http://www.sqlfiddle.com/#!18/164a4/3/0

                输出

                +------+-------+-------+-------+-------+
                | year | month | loop1 | loop2 | loop3 |
                +------+-------+-------+-------+-------+
                | 2012 |     1 |    20 |    10 |    50 |
                | 2012 |     2 |    30 |     5 |    60 |
                +------+-------+-------+-------+-------+
                

                这篇关于分解表以按列进行透视(SQL、PYSPARK)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:使用 SparkSQL 删除 MySQL 表 下一篇:在 Apache Spark 2.0.0 中,是否可以从外部数据库获取

                相关文章

                最新文章

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

                1. <small id='okGL4'></small><noframes id='okGL4'>

                  <legend id='okGL4'><style id='okGL4'><dir id='okGL4'><q id='okGL4'></q></dir></style></legend>