unpivot

Unpivot xml doc based on attributes using XSLT

I have a simple xml document that looks like the following snippet. I need to write a XSLT transform that basically 'unpivots' this document based on some of the attributes. <?xml version="1.0" encoding="utf-8" ?> <root xmlns:z="foo"> <z:row A="1" X="2" Y="n1" Z="500"/> <z:row A="2" X="5" Y="n2" Z="1500"/> </root> This is what...

Need help with SQL 2005 SELECT CASE <column> statement

Hi everyone, I would really appreciate help with this, I am seriously stuck. Basically I have a table that looks like this: SSS_DOWID Name Mon Tue Wed Thu Fri Sat Sun Description 2 M Y N N N N N N Monday 3 MF Y N N N Y N N Monday, Friday ....... 18 ...

SQL Server - Include NULL using UNPIVOT

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_...

Unpivot vs. Union queries in T-SQL 2008

Warning: This is a long question The database that I am pulling data from has the table structure like this Table: ClientSales ClientSalesId int identity (1, 1) (PK) ClientId int (FK) TermId int (FK) StudentType1Population int StudentType1Adjustment int StudentType1Sales int StudentType1SalesAdjustment int Student...

unpivot and PostgreSQL

Is there a unpivot equivalent function in PostgreSQL? ...

Updating an UNPIVOTted SQL Table

Okay, I've been beating my head against this, and I'm sure I'm just missing something obvious, but... I have a table in a customer's database, that's basically: Item_Set_Key int Item_1 bit Notes_1 nvarchar(80) Item_2 bit Notes_2 nvarchar(80) Item_3 bit Notes_3 nvarchar(80) ... There's 99 items in ea...

Pivot / unpivot in SQL

Hi, I have a view in SQL that I have generated by analysing the values in tables so that field either contain the value 'N', 'D' or 'V'. I can work out the totals by column but not by row... Is this possible? Example: Data No, Col_1, Col_2, Col_3 1, N, N, N 2, N, D, D 3, N, V, D 4, V, ...

Trouble with unpivot / pivot SQL 2005

I am having a really difficult time getting the result I need out of the following query. SELECT SUM(HEALTH_MTTR) AS MTTR, SUM(HEALTH_OTR) AS OTR, SUM(HEALTH_REPEAT) AS REPEAT, SUM(HEALTH_CHRONIC) AS CHRONIC, SUM(HEALTH_TOTAL) AS TOTAL, (CAST(CAST(YEAR([DATE_RESOLVED_FOR_CLOSURE]) AS VARCHAR(4)) + '-' + Right('00' + CAST(MONTH([DATE_RES...

sql server unpivoting table

Hi, I'm having trouble trying to unpivot the following table. | operation | ...other columns... | A1 | B1 | C1 | A2 | B2 | C2 | A3 | B3 | C3 | ... | into something like this... | operation | ...other columns... | AValue | BValue | CValue | Right now im doing it like this SELECT operation , ...other columns , Avalue, BValue , CVa...

TSQL - Help with UNPIVOT

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...

Have unpivot automatically grab column list (oracle 11g)

This is a follow up question to http://stackoverflow.com/questions/2344403/transpose-one-row-into-many-rows-oracle I want to be able to unpivot an arbitrary query result. To unpivot a table manually, I would do: select value_type, value from ( ( -- query to be unpivoted -- EG: select col1, col2, col3, col4, col5 from table ) ...

t-sql most efficient row to column? crosstab for xml path, pivot

I am looking for the most performant way to turn rows into columns. I have a requirement to output the contents of the db (not actual schema below, but concept is similar) in both fixed width and delimited formats. The below FOR XML PATH query gives me the result I want, but when dealing with anything other than small amounts of data, ...

PIVOT / UNPIVOT in SQL Server 2008

Hello 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 ...

Split Multiple Columns into Multiple Rows

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 | User...

Uing PIVOT/UNPIVOT to convert rows to columns...

Hi, I have been reading up on PIVOT an UNPIVOT but have not been able to get the results formatted correctly to present the data. Here is my source table: StepID | ShortDesc | Type_1 | ar1 | ar2 1 ShortDesc1 10 11.11 11.01 2 ...