Break down a table to pivot in columns (SQL,PYSPARK)

ε祈祈猫儿з 提交于 2019-12-11 08:55:06

问题


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|
+----+-----+-----+-----+

And I need to get an output like:

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

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

output still so far:

+----+-----+-----+-----+
|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|
+----+-----+-----+-----+

How could I do it? thanks so much


回答1:


Table Script and Sample data

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')

Using Pivot function...

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

Online Demo: 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) 

Online Demo: http://www.sqlfiddle.com/#!18/164a4/3/0

Output

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



回答2:


You don't need to use join you can do conditional aggregation:

select year, month,
       max(case when loop = 'loop1' then total end) loop1,
       max(case when loop = 'loop2' then total end) loop2,
       max(case when loop = 'loop3' then total end) loop3
from test a
group by year, month;



回答3:


You can use PIVOT() to convert rows to columns:

SELECT
    year,
    MONTH,
    p.loop1 AS 'total_loop1',
    p.loop2 AS 'total_loop2',
    p.loop3 AS 'total_loop3'
FROM
    tablename
    PIVOT
        (MAX(total)
            FOR loop IN ([loop1], [loop2], [loop3])
        ) AS p;


来源:https://stackoverflow.com/questions/50297153/break-down-a-table-to-pivot-in-columns-sql-pyspark

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!