Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Sql server 2008 query help

Posted on 2011-03-10
4
Medium Priority
?
574 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
[X]
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
  • 2
4 Comments
 
LVL 32

Assisted Solution

by:Ephraim Wangoya
Ephraim Wangoya earned 400 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 400 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 41

Accepted Solution

by:
Sharath earned 1200 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

715 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