Home Unpivot with column name
Reply: 0

Unpivot with column name

user7300 Published in September 20, 2018, 4:36 pm

I have a table StudentMarks with columns Name, Maths, Science, English. Data is like

Name,  Maths, Science, English  
Tilak, 90,    40,      60  
Raj,   30,    20,      10

I want to get it arranged like the following:

Name,  Subject,  Marks
Tilak, Maths,    90
Tilak, Science,  40
Tilak, English,  60

With unpivot I am able to get Name, Marks properly, but not able to get the column name in the source table to the Subject column in the desired result set.

How can I achieve this?

I have so far reached the following query (to get Name, Marks)

select Name, Marks from studentmarks
  Marks for details in (Maths, Science, English)

) as UnPvt
share|improve this question
  • 1
    Can you post what you've done so far? query/output. – Hart CO Sep 27 '13 at 16:43
  • added query to get Name,Marks columns. Output has all the Name, Marks combinations. – Tilak Sep 27 '13 at 16:48

2 Answers 2

active oldest votes
up vote 152 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.512082 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO