0

I have a database table called dir which has rows like:

field1   field2    filed 3
1        name      sam 
1        age       18
1        class     12
2        name      jolly
2        age       14
2        class     8
.        .         .
.        .         .
.        .         .

I want to display it as follows using my SQL query

no      name     age    class
1       sam      18     12
2       jolly    14     8
Dharman
  • 30,962
  • 25
  • 85
  • 135
trfgh
  • 1

2 Answers2

1

You can use conditional aggregation:

select field1,
       max(case when field2 = 'name' then field3 end) as name,
       max(case when field2 = 'age' then field3 end) as age,
       max(case when field2 = 'class' then field3 end) as class
from t
group by field1;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
1

You can use conditional aggregation

select field1,max(case when field2='name' then field3 end) name,
max(case when field2='age' then field3 end) age,
max(case when field2='class' then field3 end) class
 from table group by field1
Strawberry
  • 33,750
  • 13
  • 40
  • 57
Zaynul Abadin Tuhin
  • 31,407
  • 5
  • 33
  • 63