unpivot

Is Unpivot (Not Pivot) functionality available in Linq to SQL? How?

a 夏天 提交于 2019-11-30 07:31:30
I have seen posts which would fetch you pivot results but not unpivot, Need to know if there is any clean way to achieve ? If not the any workaround would do as well ? Execute this to see unpivot results in Management Studio CREATE TABLE [dbo].[Payment]( [PaymentId] [int] NOT NULL, [EmployeeId] [int] NOT NULL, [RegularHours] [decimal](18, 0) NULL, [OvertimeHOurs] [decimal](18, 0) NULL ) ON [PRIMARY] go insert into payment values (1, 1, 40, 10) insert into payment values (1, 2, 20, 0) go select * from payment select * from payment unpivot ([hours] for [paytype] in ([RegularHours],

Combining different fees columns to create PivotTable income statement? [duplicate]

别等时光非礼了梦想. 提交于 2019-11-29 13:09:19
This question already has an answer here: Convert matrix to 3-column table ('reverse pivot', 'unpivot', 'flatten', 'normalize') 3 answers I have a table which records revenue for each product, split into type of revenue: price, fees, tax etc. To analyze the data I'd like to build a PivotTable to show the revenue breakdown, e.g.: So to build this PivotTable I need to somehow create an intermediate table of the following format so the PivotTable can be used: My question is, how do I create a PivotTable from the original table automatically without manually constructing the second table? Here are

Is Unpivot (Not Pivot) functionality available in Linq to SQL? How?

烂漫一生 提交于 2019-11-29 10:32:08
问题 I have seen posts which would fetch you pivot results but not unpivot, Need to know if there is any clean way to achieve ? If not the any workaround would do as well ? Execute this to see unpivot results in Management Studio CREATE TABLE [dbo].[Payment]( [PaymentId] [int] NOT NULL, [EmployeeId] [int] NOT NULL, [RegularHours] [decimal](18, 0) NULL, [OvertimeHOurs] [decimal](18, 0) NULL ) ON [PRIMARY] go insert into payment values (1, 1, 40, 10) insert into payment values (1, 2, 20, 0) go

Convert one row into multiple rows with fewer columns

北战南征 提交于 2019-11-28 14:22:01
I'd like to convert single rows into multiple rows in PostgreSQL, where some of the columns are removed. Here's an example of the current output: name | st | ot | dt | -----|----|----|----| Fred | 8 | 2 | 3 | Jane | 8 | 1 | 0 | Samm | 8 | 0 | 6 | Alex | 8 | 0 | 0 | Using the following query: SELECT name, st, ot, dt FROM times; And here's what I want: name | t | val | -----|----|-----| Fred | st | 8 | Fred | ot | 2 | Fred | dt | 3 | Jane | st | 8 | Jane | ot | 1 | Samm | st | 8 | Samm | dt | 6 | Alex | st | 8 | How can I modify the query to get the above desired output? SELECT times.name, x.t,

SQL Server convert columns to rows

感情迁移 提交于 2019-11-28 12:18:53
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? 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.PValue1), ('Value2', t.Value2, t.PValue2) ) x (Col, Value, PValue) where x.Value <> x.PValue; See SQL Fiddle

SQL Server 2008 : Convert column value to row

此生再无相见时 提交于 2019-11-28 11:49:43
I have a table in the following format Country_Code | 1960 | 1961 | 1962 | ..... | 2011 ------------------------------------------------ IND | va11 | va12 | va13 | ..... | va1x AUS | va21 | va22 | va23 | ..... | va2x ENG | va31 | va32 | va33 | ..... | va3x I want to convert it into the below format Country_Code | Year | Value --------------------------- IND | 1960 | va11 IND | 1961 | va12 IND | 1962 | va13 . . IND | 2011 | va1x AUS | 1960 | va21 AUS | 1961 | va22 AUS | 1962 | va23 . . AUS | 2011 | va2x ENG | 1960 | va31 ENG | 1961 | va32 ENG | 1962 | va33 . . ENG | 2011 | va3x How can this be

Convert row data to column in SQL Server

和自甴很熟 提交于 2019-11-28 11:46:44
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') INSERT INTO STAGING VALUES (1, 'CHILD', 'JACK') INSERT INTO STAGING VALUES (2, 'CHILD', 'KAYALA') I

How to use dynamic SQL to add value of 2 columns

微笑、不失礼 提交于 2019-11-28 11:23:00
问题 I have small table which contains students marks. Table data is shown in below image. It is look like below in excel I want to calculate the total using dynamic SQL. I don't want to update it. However, I just want to select all the data with calculated total using dynamic SQL. Please refer below code: DECLARE @SQL NVARCHAR(MAX)='' DECLARE @SNumberList NVARCHAR(MAX)='' DECLARE @CalculatedLineNumbers NVARCHAR(MAX)='' SELECT @CalculatedLineNumbers = @CalculatedLineNumbers+ ', '+ CASE WHEN SNo =

SQL Unpivot multiple columns Data

为君一笑 提交于 2019-11-28 09:46:09
I am using SQL server 2008 and I am trying to unpivot the data. Here is the SQL code that I am using, CREATE TABLE #pvt1 (VendorID int, Sa int, Emp1 int,Sa1 int,Emp2 int) GO INSERT INTO #pvt1 VALUES (1,2,4,3,9); GO --Unpivot the table. SELECT distinct VendorID,Orders,Orders1 FROM (SELECT VendorID, Emp1, Sa,Emp2,Sa1 FROM #pvt1 ) p UNPIVOT (Orders FOR Emp IN (Emp1,Emp2) )AS unpvt UNPIVOT (Orders1 FOR Emp1 IN (Sa,Sa1) )AS unpvt1; GO And Here is the result of the above code. VendorID Orders Orders1 1 4 2 1 4 3 1 9 2 1 9 3 But I want my Output to be the way indicated below VendorID Orders Orders1 1

SQL Server - Include NULL using UNPIVOT

时光怂恿深爱的人放手 提交于 2019-11-28 08:16:27
UNPIVOT will not return NULLs, but I need them in a comparison query. I am trying to avoid using ISNULL the following example (Because in the real sql there are over 100 fields.: Select ID, theValue, column_name From (select ID, ISNULL(CAST([TheColumnToCompare] AS VarChar(1000)), '') as TheColumnToCompare from MyView where The_Date = '04/30/2009' ) MA UNPIVOT (theValue FOR column_name IN ([TheColumnToCompare]) ) AS unpvt Any alternatives? It's a real pain. You have to switch them out before the UNPIVOT , because there is no row produced for ISNULL() to operate on - code generation is your