unpivot

unpivot and PostgreSQL

我与影子孤独终老i 提交于 2019-11-26 01:58:57
问题 Is there a unpivot equivalent function in PostgreSQL? 回答1: 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 thing FROM foo UNION ALL SELECT id, 'c' AS colname, c AS thing FROM foo ORDER BY id; This runs 3 different subqueries on foo , one for

Unpivot in spark-sql/pyspark

人走茶凉 提交于 2019-11-26 01:43:24
问题 I have a problem statement at hand wherein I want to unpivot table in spark-sql/pyspark. I have gone through the documentation and I could see there is support only for pivot but no support for un-pivot so far. Is there a way I can achieve this? Let my initial table look like this: when I pivot this in pyspark using below mentioned command: df.groupBy(\"A\").pivot(\"B\").sum(\"C\") I get this as the output: Now I want to unpivot the pivoted table. In general this operation may/may not yield

SQL transpose full table

丶灬走出姿态 提交于 2019-11-26 00:55:14
问题 I need to do the following transpose in MS SQL from: Day A B --------- Mon 1 2 Tue 3 4 Wed 5 6 Thu 7 8 Fri 9 0 To the following: Value Mon Tue Wed Thu Fri -------------------------- A 1 3 5 7 9 B 2 4 6 8 0 I understand how to do it with PIVOT when there is only one column (A) but I can not figure out how to do it when there are multiple columns to transpose (A,B,...) Example code to be transposed: select LEFT(datename(dw,datetime),3) as DateWeek, sum(ACalls) as A, Sum(BCalls) as B from

MySQL - turn table into different table

陌路散爱 提交于 2019-11-25 23:38:43
问题 I\'m probably not seeing things very clear at this moment, but I have a table in MySQL which looks like this: ID | a | b | c 1 | a1 | b1 | c1 2 | a2 | b2 | c2 For some reason (actually a join on another table - based on ID , but I think if someone can help me out with this part, I can do the rest myself), I needed those rows to be like this instead: 1 | a1 | a 1 | b1 | b 1 | c1 | c 2 | a2 | a 2 | b2 | b 2 | c2 | c So basically, I need to view the rows like: ID , columntitle , value Is there

Unpivot with column name

亡梦爱人 提交于 2019-11-25 22:48:37
问题 I have a table StudentMarks with columns Name, Maths, Science, English . Data is like Name, Maths, Science, English Tilak, 90, 40, 60 Raj, 30, 20, 10 I want to get it arranged like the following: Name, Subject, Marks Tilak, Maths, 90 Tilak, Science, 40 Tilak, English, 60 With unpivot I am able to get Name, Marks properly, but not able to get the column name in the source table to the Subject column in the desired result set. How can I achieve this? I have so far reached the following query

Unpivot in spark-sql/pyspark

我怕爱的太早我们不能终老 提交于 2019-11-25 22:48:30
I have a problem statement at hand wherein I want to unpivot table in spark-sql/pyspark. I have gone through the documentation and I could see there is support only for pivot but no support for un-pivot so far. Is there a way I can achieve this? Let my initial table look like this: when I pivot this in pyspark using below mentioned command: df.groupBy("A").pivot("B").sum("C") I get this as the output: Now I want to unpivot the pivoted table. In general this operation may/may not yield the original table based on how I've pivoted the original table. Spark-sql as of now doesn't provide out of

SQL Server : Columns to Rows

偶尔善良 提交于 2019-11-25 22:37:01
问题 Looking for elegant (or any) solution to convert columns to rows. Here is an example: I have a table with the following schema: [ID] [EntityID] [Indicator1] [Indicator2] [Indicator3] ... [Indicator150] Here is what I want to get as the result: [ID] [EntityId] [IndicatorName] [IndicatorValue] And the result values will be: 1 1 \'Indicator1\' \'Value of Indicator 1 for entity 1\' 2 1 \'Indicator2\' \'Value of Indicator 2 for entity 1\' 3 1 \'Indicator3\' \'Value of Indicator 3 for entity 1\' 4

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

放肆的年华 提交于 2019-11-25 21:41:39
问题 I need to convert the Excel matrix FIRST in the table LATER : FIRST : P1 P2 P3 P4 F1 X F2 X X F3 X X F4 X X LATER : F P VALUE F1 P1 X F1 P2 F1 P3 F1 P4 F2 P1 X F2 P2 X F2 P3 F2 P4 F3 P1 F3 P2 X F3 P3 F3 P4 X F4 P1 F4 P2 X F4 P3 X F4 P4 回答1: 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

SQL transpose full table

浪尽此生 提交于 2019-11-25 19:32:01
I need to do the following transpose in MS SQL from: Day A B --------- Mon 1 2 Tue 3 4 Wed 5 6 Thu 7 8 Fri 9 0 To the following: Value Mon Tue Wed Thu Fri -------------------------- A 1 3 5 7 9 B 2 4 6 8 0 I understand how to do it with PIVOT when there is only one column (A) but I can not figure out how to do it when there are multiple columns to transpose (A,B,...) Example code to be transposed: select LEFT(datename(dw,datetime),3) as DateWeek, sum(ACalls) as A, Sum(BCalls) as B from DataTable group by LEFT(datename(dw,datetime),3) Table Structure: Column DataType DateTime Datetime ACalls