Solved

Sql server 2008 query help

Posted on 2011-03-10
4
495 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
Comment Utility

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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.

762 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