Solved

Sql server 2008 query help

Posted on 2011-03-10
4
513 Views
Last Modified: 2012-05-11
Hi,
i have a following table. I want to display some column as rows. Please help me achieving this.
Here is the sample table
EmployeeTable
---------------
EmpID  2008Scores 2009Scores   2010Scores
2             12               14                18

I want the result like this

EmpID  Year    ScoreValue
2           2008     12
2          2009      14
2          2010      18

Please help.

Thanks.
0
Comment
Question by:r_pat72
  • 2
4 Comments
 
LVL 32

Assisted Solution

by:ewangoya
ewangoya earned 100 total points
ID: 35102997

you can use Pivot or just do a union like this

select EmpID , [Year], 2008Scores  ScoreValue
from Table1
union all
select EmpID , [Year], 2009Scores  ScoreValue
from Table1
union all
select EmpID , [Year], 2010Scores  ScoreValue
from Table1
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 35103078
select empid, Year = '2008', 2008Scores as ScoreValue
where empid = 2
union
select empid, Year = '2009', 2009Scores as ScoreValue
where empid = 2
union
select empid, Year = '2010', 2010Scores as ScoreValue
where empid = 2
0
 
LVL 29

Assisted Solution

by:Paul Jackson
Paul Jackson earned 100 total points
ID: 35103087
correction:

select empid, Year = '2008', 2008Scores as ScoreValue
from employeetable
where empid = 2
union
select empid, Year = '2009', 2009Scores as ScoreValue
from employeetable
where empid = 2
union
select empid, Year = '2010', 2010Scores as ScoreValue
from employee table
where empid = 2
0
 
LVL 40

Accepted Solution

by:
Sharath earned 300 total points
ID: 35103157
Try unpivot.
select EmpID,REPLACE([Year],'Score','') [Year],Score
  from your_table 
 unpivot (Score for [Year] in ([2008Score],[2009Score],[2010Score])) p

Open in new window

declare @table table(EmpID int,[2008Score] int,[2009Score] int,[2010Score] int)
insert @table values(2,12,14,18)
select EmpID,REPLACE([Year],'Score','') [Year],Score
  from @table 
 unpivot (Score for [Year] in ([2008Score],[2009Score],[2010Score])) p
/*
EmpID	Year	Score
2	2008	12
2	2009	14
2	2010	18
*/

Open in new window

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

820 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