unpivot

Convert row data to column in SQL Server

徘徊边缘 提交于 2019-12-17 19:55:15
问题 Today I was asked by my colleague to transform data from a vertical staging table into a horizontal table. I mean transform rows to column. I used PIVOT and resolved it. But got into situation where I am getting trouble to move data if the data field repeats itself. Here is the test data that I am working on: CREATE TABLE STAGING ( ENTITYID INT, PROPERTYNAME VARCHAR(25), PROPERTYVALUE VARCHAR(25) ) INSERT INTO STAGING VALUES (1, 'NAME', 'DONNA') INSERT INTO STAGING VALUES (1, 'SPOUSE', 'HENRY

SQL Server convert columns to rows

为君一笑 提交于 2019-12-17 19:46:28
问题 I have a sql table with current value and previous value. Id Value1 PValue1 Value2 PValue2 1 A A V V1 2 B B1 W W1 3 C C1 X X I want to compare them and display in a the following table if the value has changes. Id Column Value Pvalue 1 Value2 V V1 2 Value1 B B1 2 Value2 W W1 3 Value1 C C1 Is it possible in SQL 2008 without looping each column? 回答1: You can use a CROSS APPLY to unpivot the data: SELECT t.id, x.Col, x.Value, x.PValue FROM YourTable t CROSS APPLY ( VALUES ('Value1', t.Value1, t

How to unpivot a table in PostgreSQL

时光怂恿深爱的人放手 提交于 2019-12-17 17:06:49
问题 I am having difficulties writing a Postgres function, as I am not familiar with it. I have multiple tables to import into Postgres with this format: id | 1960 | 1961 | 1962 | 1963 | ... ____________________________________ 1 23 45 87 99 2 12 31 ... which I need to convert into this format: id | year | value _________________ 1 1960 23 1 1961 45 1 1962 87 ... 2 1960 12 2 1961 31 ... I would imagine the function too to read like this: SELECT all-years FROM imported_table; CREATE a new_table;

Mysql Convert Column to row (Pivot table )

坚强是说给别人听的谎言 提交于 2019-12-17 06:38:21
问题 I have a table like this +---+-----+----+----+----+----+ |id |month|col1|col2|col3|col4| +---+-----+----+----+----+----+ |101|Jan |A |B |NULL|B | +---+-----+----+----+----+----+ |102|feb |C |A |G |E | +---+-----+----+----+----+----+ And then I want to create report like this +----+---+---+ |desc|jan|feb| +----+---+---+ |col1|A |C | +----+---+---+ |col2|B |A | +----+---+---+ |col3|0 |G | +----+---+---+ |Col4|B |E | +----+---+---+ Can anyone help with this? 回答1: What you need to do is first,

SQL Unpivot table

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-13 21:21:46
问题 I am facing problem on unpivot sql statement, below is the table how its look like: ID A0001 A0002 A0003 == =========== ========== ========== S1 100 200 300 S2 321 451 234 S3 0 111 222 I want to pivot A0001,A0002 and A0003. Create 3 more column for HEADER,SEQUENCE AND DATA. Below is my expected table to become like this: ID HEADER SEQUENCE DATA == ========== =========== ======= S1 A0001 1 100 S1 A0001 2 200 S1 A0001 3 300 S2 A0002 1 321 S2 A0002 2 451 S2 A0002 3 234 S3 A0003 1 111 S3 A0003 2

SQL 2008 Combining data from multiple rows with the same ID into one row

狂风中的少年 提交于 2019-12-13 12:21:19
问题 I have data in one table that contains several rows of data for the same CardNum. I would like to create a table where all the data for the same CardNum is displayed on the same row. My data is currently like this: PartID | CardNumber | RdrGrpID | TZID 0 412 31 1 0 412 34 1 0 567 38 1 0 567 33 5 0 567 71 3 This is how I would like the data to be: PartID | CardNumber | RdrGrpID_1 | TZID_1 | RdrGrpID_2 | TZID_2 | RdrGrpID_3 | TZID_3 0 412 31 1 34 1 0 567 38 1 33 5 71 3 Thank you in advance. 回答1

T-SQL (Un)Pivot Table

流过昼夜 提交于 2019-12-13 09:45:30
问题 I have a view as follows (did a view as I thought it would be easier that accessing the more complicated table) ID | aText1 | aText2 | aInt1 | aInt2 ------------------------------------- 1 | ABC1 | XYZ1 | 2 | 20 2 | ABC1 | XYZ2 | 3 | 25 3 | ABC2 | XYZ2 | 1 | 30 4 | ABC2 | XYZ1 | 4 | 35 I need the result to read | XYZ1 | XYZ2 aText1 | aInt1 | aInt2 | aInt1 | aInt2 --------------------------------------- ABC1 | 2 | 20 | 3 | 25 ABC2 | 1 | 30 | 4 | 35 I've tried various pivots but all fail.

T-SQL Pivot/Unpivot(Transpose) Column Headers Needed as Data Rows

白昼怎懂夜的黑 提交于 2019-12-13 09:25:53
问题 I'm working on a T-SQL issue where I needed to Transponse Rows into Columns and using UNPIVOT and PIVOT together per a post at Simple way to transpose columns and rows in Sql? No problem. It does Exactly what I want with the row/column manipulation. However what I would REALLY like to do is to get the values used for the column headers to become yet another row in the results. My abbreviated code is: SELECT * FROM (SELECT fiscalyear, Sum(totalrecords) AS TotalRecords FROM dbo

How to convert columns to rows?

旧城冷巷雨未停 提交于 2019-12-13 07:27:22
问题 I have a table like this one RowNum | TranNo | nTotalSales | nBalance 1 | 1 | 800 | 0 and I want to display it this way RowNum | 1 cTranNo | 1 nTotalSales | 800 nBalance | 0 How can I do this? 回答1: Here is a complete working example, when you you do an UNPIVOT , which is what your are asking for, your 'value' types need to be of the same type, so cast them however you want. In my example, I have cast them all to VARCHAR(20): DECLARE @bob TABLE ( RowNum INT, TranNo INT, nTotalSales INT,

Pivot Query - Missing records

那年仲夏 提交于 2019-12-13 06:24:14
问题 I have a pivot query which works well until now, but there has been a requirement where I need to specify a condition in the unpivot query which would omit the rows and column values which are not matching with the condition and displays me null. However, I need to show those value as a part of final results. I have tried to do union to include the missing values, but won't get the desired output. I am pasting my pivot query below along with the results which are displayed and which needs to