0

My query like this :

SELECT a.number, a.description, b.attribute_code, b.attribute_value 
FROM items a 
JOIN attr_maps b ON b.number = a.number WHERE a.number = AB123

If the query executed, the result like this :

enter image description here

I want to make the result like this :

enter image description here

How can I do it?

moses toh
  • 10,726
  • 57
  • 212
  • 388

1 Answers1

1

You can use conditional aggregation:

SELECT i.number, i.description,
       MAX(CASE WHEN am.attribute_code = 'brand' then am.attribute_value END) as brand,
       MAX(CASE WHEN am.attribute_code = 'model' then am.attribute_value END) as model,
       MAX(CASE WHEN am.attribute_code = 'category' then am.attribute_value END) as category,
       MAX(CASE WHEN am.attribute_code = 'subcategory' then am.attribute_value END) as subcategory
FROM items i JOIN
     attr_maps am
     ON am.number = i.number
WHERE i.number = AB123
GROUP BY i.number, i.description
Gordon Linoff
  • 1,198,228
  • 53
  • 572
  • 709