Solved

Sql server 2008 query help

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSIS with VPN COnnection 2 77
SQL - Copy data from one database to another 6 19
performance query 4 24
T-SQL:  Collapsing 9 25
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

810 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