0

I have a DB table with records as shown below,

ID   ATTR_NAME   ATTR_VALUE 
1    ABC          DEF
1    GHI          JKL
1    MNO          PQR

I would like to get a single row as

ID  ABC GHI  MNO
1   DEF JKL  PQR
OGHaza
  • 4,795
  • 7
  • 23
  • 29

2 Answers2

0

It may be a little fragile and not that future proofed, but Pivot can give you what you want:

SELECT *
FROM (
   SELECT attr_name, attr_value
   FROM   test
)
PIVOT
(  MIN(attr_value)
   FOR attr_name IN ( 'ABC','GHI','MNO' )
)

However, I'd advise that you consider if you really need it in that format and see if you can get it out in a more natural format.

Rob Baillie
  • 3,436
  • 2
  • 20
  • 34
0
select ID, 
   sum(case when attr_name = 'ABC' then attr_value end) as ABC, 
   sum(case when attr_name = 'GHI' then attr_value end) as GHI, 
   sum(case when attr_name = 'MNO' then attr_value end) as MNO
from DB
group by ID;
belwood
  • 3,320
  • 11
  • 38
  • 45
Jing
  • 1