unpivot

Transpose or unpivot every other column

蹲街弑〆低调 提交于 2019-12-02 02:31:25
问题 Basically what I am trying to do is transpose every other column to a row with the following columns data beside it. The source can have only two columns up to one-thousand and millions of rows. I am only doing this on a per-row basis. I have tried doing a "UNION ALL" however this is incredibly slow when dealing with hundreds of columns in a large table. Possible solutions are using UNPIVOT although every example I can find is doing something different from what I want. Example: Before Query

Transpose or unpivot every other column

妖精的绣舞 提交于 2019-12-02 01:21:35
Basically what I am trying to do is transpose every other column to a row with the following columns data beside it. The source can have only two columns up to one-thousand and millions of rows. I am only doing this on a per-row basis. I have tried doing a "UNION ALL" however this is incredibly slow when dealing with hundreds of columns in a large table. Possible solutions are using UNPIVOT although every example I can find is doing something different from what I want. Example: Before Query Columns... Apple | Apple Data | Banana | Banana Data | Cranberry | Cranberry Data | .... Data Returned.

Converting columns to rows (UNPIVOT) in hiveql

风流意气都作罢 提交于 2019-12-02 00:31:16
问题 I have a table with a structure like this: column1, column2, column3, X1, X2, X3, X4 A1, A2, A3, 5, 6, 1, 4 I would like to convert this into column1, column2, column3, Key, Value A1, A2, A3, X1, 5 A1, A2, A3, X2, 6 A1, A2, A3, X3, 1 A1, A2, A3, X4 4 I was able to do this already using 4 queries stitched together with "UNION ALL", but since the table is huge and each select translates into a lengthy map-reduce, using UNION makes the query takes N times the time it should ideally take. Where N

SQL - Union All Users in One Table

不羁的心 提交于 2019-12-01 19:46:07
Table : Popular UserName FriendName -------- ---------- John Sarah Philip Ursula John Marry John Jeremy Philip Brock Khan Lemy And I want list with query; John Philip Khan -------- ---------- -------- Sarah Ursula Lemy Marry Brock -NULL- Jeremy -NULL- -NULL- I have 100+ Username... help me for to list with SQL Query (MSSQL) ChrisCarroll If you have "100+ UserNames" you will want this to be DYNAMIC so that you don't have to type out specific CASE statements for each UserName. Also you won't want to have to update your script every time a new UserName is added to your table. The below script

SQL - Union All Users in One Table

人盡茶涼 提交于 2019-12-01 19:14:14
问题 Table : Popular UserName FriendName -------- ---------- John Sarah Philip Ursula John Marry John Jeremy Philip Brock Khan Lemy And I want list with query; John Philip Khan -------- ---------- -------- Sarah Ursula Lemy Marry Brock -NULL- Jeremy -NULL- -NULL- I have 100+ Username... help me for to list with SQL Query (MSSQL) 回答1: If you have "100+ UserNames" you will want this to be DYNAMIC so that you don't have to type out specific CASE statements for each UserName. Also you won't want to

Postgres: convert single row to multiple rows (unpivot)

烂漫一生 提交于 2019-12-01 18:08:17
I have a table: Table_Name: price_list --------------------------------------------------- | id | price_type_a | price_type_b | price_type_c | --------------------------------------------------- | 1 | 1234 | 5678 | 9012 | | 2 | 3456 | 7890 | 1234 | | 3 | 5678 | 9012 | 3456 | --------------------------------------------------- I need a select query in Postgres which gives result like this: --------------------------- | id | price_type | price | --------------------------- | 1 | type_a | 1234 | | 1 | type_b | 5678 | | 1 | type_c | 9012 | | 2 | type_a | 3456 | | 2 | type_b | 7890 | | 2 | type_c |

Handle NULL value in UNPIVOT

北城余情 提交于 2019-12-01 17:30:46
I'm able to unpivot a table but null values are not included in the result. create table pivot_task ( age int null, [a] numeric(8,2), [b] numeric(8,2), [c] numeric(8,2), [d] numeric(8,2), [e] numeric(8,2) ); select * from pivot_task; insert into pivot_task values (18, 0.5, null, 0.6, 1.21, 1.52), (19, 7.51, 6.51, 5.51, null, 3.53), (20, 4.52, 4.52, 6.52, 3.53, null); select age, [over], [av] from pivot_task unpivot ( [av] for [over] in ([a], [b], [c], [d], [e]) ) a; You can see the result on http://sqlfiddle.com/#!6/2ab59/1 for 18 age [over] b and its null value is missing I want to include

TSQL - Help with UNPIVOT

别来无恙 提交于 2019-12-01 17:12:48
I am transforming data from this legacy table: Phones(ID int, PhoneNumber, IsCell bit, IsDeskPhone bit, IsPager bit, IsFax bit) These bit fields are not nullables and, potentially, all four bit fields can be 1. How can I unpivot this thing so that I end up with a separate row for each bit field = 1 . For instance, if the original table looks like this... ID, PhoneNumber, IsCell, IsPager, IsDeskPhone, IsFax ---------------------------------------------------- 1 123-4567 1 1 0 0 2 123-6567 0 0 1 0 3 123-7567 0 0 0 1 4 123-8567 0 0 1 0 ... I want the result to be the following: ID PhoneNumber

TSQL - Help with UNPIVOT

荒凉一梦 提交于 2019-12-01 16:52:20
问题 I am transforming data from this legacy table: Phones(ID int, PhoneNumber, IsCell bit, IsDeskPhone bit, IsPager bit, IsFax bit) These bit fields are not nullables and, potentially, all four bit fields can be 1. How can I unpivot this thing so that I end up with a separate row for each bit field = 1 . For instance, if the original table looks like this... ID, PhoneNumber, IsCell, IsPager, IsDeskPhone, IsFax ---------------------------------------------------- 1 123-4567 1 1 0 0 2 123-6567 0 0

Handle NULL value in UNPIVOT

假装没事ソ 提交于 2019-12-01 15:27:36
问题 I'm able to unpivot a table but null values are not included in the result. create table pivot_task ( age int null, [a] numeric(8,2), [b] numeric(8,2), [c] numeric(8,2), [d] numeric(8,2), [e] numeric(8,2) ); select * from pivot_task; insert into pivot_task values (18, 0.5, null, 0.6, 1.21, 1.52), (19, 7.51, 6.51, 5.51, null, 3.53), (20, 4.52, 4.52, 6.52, 3.53, null); select age, [over], [av] from pivot_task unpivot ( [av] for [over] in ([a], [b], [c], [d], [e]) ) a; You can see the result on