Solved

Sql server 2008 query help

Posted on 2011-03-10
4
500 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

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

943 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now