Equivalent to unpivot() in PostgreSQL

Create an example table: CREATE TEMP TABLE foo (id int, a text, b text, c text); INSERT INTO foo VALUES (1, ‘ant’, ‘cat’, ‘chimp’), (2, ‘grape’, ‘mint’, ‘basil’); You can ‘unpivot’ or ‘uncrosstab’ using UNION ALL: SELECT id, ‘a’ AS colname, a AS thing FROM foo UNION ALL SELECT id, ‘b’ AS colname, b AS … Read more

SQL transpose full table

In order to transpose the data into the result that you want, you will need to use both the UNPIVOT and the PIVOT functions. The UNPIVOT function takes the A and B columns and converts the results into rows. Then you will use the PIVOT function to transform the day values into columns: select * … Read more

Unpivot in spark-sql/pyspark

You can use the built in stack function, for example in Scala: scala> val df = Seq((“G”,Some(4),2,None),(“H”,None,4,Some(5))).toDF(“A”,”X”,”Y”, “Z”) df: org.apache.spark.sql.DataFrame = [A: string, X: int … 2 more fields] scala> df.show +—+—-+—+—-+ | A| X| Y| Z| +—+—-+—+—-+ | G| 4| 2|null| | H|null| 4| 5| +—+—-+—+—-+ scala> df.select($”A”, expr(“stack(3, ‘X’, X, ‘Y’, Y, ‘Z’, … Read more

How do you create a “reverse pivot” in Google Sheets?

I wrote a simple general custom function, which is 100% reusable you can unpivot / reverse pivot a table of any size. In your case you could use it like this: =unpivot(A1:D4,1,1,”customer”,”sales”) So you can use it just like any built-in array function in spreadsheet. Please see here 2 examples: https://docs.google.com/spreadsheets/d/12TBoX2UI_Yu2MA2ZN3p9f-cZsySE4et1slwpgjZbSzw/edit#gid=422214765 The following is the … Read more

SQL Server : Columns to Rows

You can use the UNPIVOT function to convert the columns into rows: select id, entityId, indicatorname, indicatorvalue from yourtable unpivot ( indicatorvalue for indicatorname in (Indicator1, Indicator2, Indicator3) ) unpiv; Note, the datatypes of the columns you are unpivoting must be the same so you might have to convert the datatypes prior to applying the … Read more

Convert matrix to 3-column table (‘reverse pivot’, ‘unpivot’, ‘flatten’, ‘normalize’)

To “reverse pivot”, “unpivot” or “flatten”: For Excel 2003: Activate any cell in your summary table and choose Data – PivotTable and PivotChart Report: For later versions access the Wizard with Alt+D, P. For Excel for Mac 2011, it’s ⌘+Alt+P (See here). Select Multiple consolidation ranges and click Next. In “Step 2a of 3”, choose … Read more