unpivot

Efficiently reformat data layout

耗尽温柔 提交于 2019-12-01 14:50:49
I have several Excel spreadsheets with data layout like this raw data : company company1 company2 company3 currency $ Y E 1/1/2013 32.68 12 3 1/2/2013 12.5 13 4 1/3/2013 45 45 8 which basically are time series data grouped together. I need the final layout transformed into panel data, like this wanted panel data : Since my observations are usually very large, it is not practical manually to reformat it. Is there a macro code that can achieve such a goal? pnuts Turn on Record Macro if desired. In Excel, move the currency row out of the way. 'Reverse pivot' (as detailed here ), sort the Table on

Efficiently reformat data layout

余生颓废 提交于 2019-12-01 13:27:49
问题 I have several Excel spreadsheets with data layout like this raw data: company company1 company2 company3 currency $ Y E 1/1/2013 32.68 12 3 1/2/2013 12.5 13 4 1/3/2013 45 45 8 which basically are time series data grouped together. I need the final layout transformed into panel data, like this wanted panel data: Since my observations are usually very large, it is not practical manually to reformat it. Is there a macro code that can achieve such a goal? 回答1: Turn on Record Macro if desired. In

PIVOT / UNPIVOT in SQL Server 2008

*爱你&永不变心* 提交于 2019-12-01 12:08:04
问题 I got child / parent tables as below. MasterTable: MasterID, Description ChildTable ChildID, MasterID, Description. Using PIVOT / UNPIVOT how can i get result as below in single row. if (MasterID : 1 got x child records) MasterID, ChildID1, Description1, ChildID2, Description2....... ChildIDx, Descriptionx Thanks 回答1: here is a T_SQL, assuming this: You do not know how many columns may appear in the results. The Pivot elements can vary (thats why the first assumption). You need the specific

Unpivot ALL Columns in a SQL Table

瘦欲@ 提交于 2019-12-01 10:54:55
I have a table with 30 columns and I want to easily unpivot ALL columns. I understand I can use this strategy: SELECT col, value INTO New_Table FROM (SELECT * FROM Test_Data) p UNPIVOT (value FOR col IN (Column_Name1, Column_Name2... Column_Name30)) as unpvt This is how my data comes in: Column_Name1 Column_Name2 Column_Name3 Value11 Value21 Value31 Value12 Value22 Value32 Value13 Value23 Value33 This is how I want to store it in the new table: New_Column1 New_cloumn2 Column_Name1 Value11 Column_Name1 Value12 Column_Name1 Value13 Column_Name2 Value21 ... But there must be an easier way than

Unpivot ALL Columns in a SQL Table

纵然是瞬间 提交于 2019-12-01 09:55:54
问题 I have a table with 30 columns and I want to easily unpivot ALL columns. I understand I can use this strategy: SELECT col, value INTO New_Table FROM (SELECT * FROM Test_Data) p UNPIVOT (value FOR col IN (Column_Name1, Column_Name2... Column_Name30)) as unpvt This is how my data comes in: Column_Name1 Column_Name2 Column_Name3 Value11 Value21 Value31 Value12 Value22 Value32 Value13 Value23 Value33 This is how I want to store it in the new table: New_Column1 New_cloumn2 Column_Name1 Value11

transpose column to row oracle

半腔热情 提交于 2019-12-01 01:21:38
I have a query returned value in this form (query return more than 50 columns). 1-99transval 100-200transval 200-300transval ... 1-99nontransval 100... 50 90 80 67 58 For a row value. I want these details to be converted into columns and take the following shape: Range Transval NonTransval 1-99 50 67 100-200 90 58 In pure SQL , it will need a lot of coding because you will have to manually put the range as there is no relation between the values and the range at all. Had there been a relationship, you could use CASE expression and build the range dynamically . SQL> WITH DATA AS 2 (SELECT 50 "1

transpose column to row oracle

不问归期 提交于 2019-11-30 20:41:51
问题 I have a query returned value in this form (query return more than 50 columns). 1-99transval 100-200transval 200-300transval ... 1-99nontransval 100... 50 90 80 67 58 For a row value. I want these details to be converted into columns and take the following shape: Range Transval NonTransval 1-99 50 67 100-200 90 58 回答1: In pure SQL , it will need a lot of coding because you will have to manually put the range as there is no relation between the values and the range at all. Had there been a

Split Multiple Columns into Multiple Rows

☆樱花仙子☆ 提交于 2019-11-30 18:27:29
I have a table with this structure. UserID | UserName | AnswerToQuestion1 | AnswerToQuestion2 | AnswerToQuestion3 1 | John | 1 | 0 | 1 2 | Mary | 1 | 1 | 0 I can't figure out what SQL query I would use to get a result set like this: UserID | UserName | QuestionName | Response 1 | John | AnswerToQuestion1 | 1 1 | John | AnswerToQuestion2 | 0 1 | John | AnswerToQuestion3 | 1 2 | Mary | AnswerToQuestion1 | 1 2 | Mary | AnswerToQuestion2 | 1 2 | Mary | AnswerToQuestion3 | 0 I'm trying to split the three columns into three separate rows. Is this possible? SELECT Y.UserID, Y.UserName, QuestionName =

SQL Server Pivot Table with Counts and Sums

最后都变了- 提交于 2019-11-30 15:10:53
问题 I am trying to get an SQL Server Pivot table to work that allows me to count and then sum a number of columns (6 in total). The purpose of the pivot table is to aggregate online questionnaire results for any number of production sites. There are 6 questions which can have 3 result values - Target, Action and Fail. What I am trying to do is count up the number of Target, Action and Fail for each question, and to then sum this up for each. So, for example, Production Site A could have 2 Target,

SQL Server Pivot Table with Counts and Sums

巧了我就是萌 提交于 2019-11-30 13:34:18
I am trying to get an SQL Server Pivot table to work that allows me to count and then sum a number of columns (6 in total). The purpose of the pivot table is to aggregate online questionnaire results for any number of production sites. There are 6 questions which can have 3 result values - Target, Action and Fail. What I am trying to do is count up the number of Target, Action and Fail for each question, and to then sum this up for each. So, for example, Production Site A could have 2 Target, 2 Action and 2 Fail. My understanding is that the SQL Server Pivot table could deliver this