Amalraj
Amalraj

Reputation: 1

Unpivot with new column name

input table

Name col1 col2 col3 MaxMarkSub
Tilak 90 40 60 col1
Raj 30 50 10 col2
Amal 90 40 60 col3
Ramesh 30 50 10 col2

I want to get it arranged like the following:

Name col1 col2 col3 mapping output
Tilak 90 40 60 col1 90
Raj 30 50 10 col2 50
Amal 90 40 60 col3 60
Ramesh 30 65 10 col2 65

How can I achieve this?

Upvotes: 0

Views: 150

Answers (1)

Venkataraman R
Venkataraman R

Reputation: 12969

You can go for simple case expression.

SELECT name, col1, col2, col3, MaxMarkSub AS mapping, 
case MaxMarkSub when 'col1' then col1
               when 'col2' then col2
               when 'col3' then col3
               end as output
from TableName

Upvotes: 0

Related Questions