Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Un-rotate Table?

Posted on 2012-04-04
Medium Priority
Last Modified: 2012-04-04
I have a table that has fields named similar to:
.  (field names between Sales_03 and Sales_10)

What I need is to un-rotate the table so that I have a new (display) field called "month" and the Sales_?? fields separated by month and just called Sales.

So, if my table has data such as this:
Company Employee Year Sales_01 Sales_02 Sales_03 Sales_04 Sales_05 (trimmed out, but you get the idea)
ABC     12345    2011 12345.67 62534.23 98737.32 92873.34 12343.30 

Open in new window

The resulting data will look like this:
Company Employee Year Month Sales
ABC     12345    2011     1 12345.67 
ABC     12345    2011     2 62534.23 
ABC     12345    2011     3 98737.32 
ABC     12345    2011     4 92873.34 
ABC     12345    2011     5 12343.30 

Open in new window

I thought the T-SQL command "UNPIVOT" would work, but I'm not sure how to use it, and how the month field would appear.

Question by:Clif
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 18

Accepted Solution

lludden earned 2000 total points
ID: 37807274
Something like this:
DECLARE @T TABLE (Company VARCHAR(10),Employee INT, [year] INT, Sales01 INT, Sales02 INT, Sales03 INT, Sales04 INT)
INSERT INTO @T SELECT 'ABC',12345,2011,555,444,666,888

SELECT Company, Employee, year, cast(RIGHT(Sales,2) AS INT) AS [Month], SalesAmt
UNPIVOT (SalesAmt FOR Sales IN (Sales01, Sales02, Sales03, Sales04)) AS uv
) T1

Open in new window

LVL 10

Author Closing Comment

ID: 37807492
That was perfect, thanks.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question