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

I use conditional aggregation like this :

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

It works

But i'm still confused to add where condition. So I want it can filter by brand etc

I try like this :

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 brand = 'honda'
GROUP BY i.number, i.description

There exist error Unknown column 'brand' in 'where clause'

How can I solve the error?

Community
  • 1
  • 1
moses toh
  • 12,344
  • 71
  • 243
  • 443

1 Answers1

2

Change the last part of your query

WHERE brand = 'honda'
GROUP BY i.number, i.description

to

GROUP BY i.number, i.description
HAVING brand = 'honda'
EvE
  • 734
  • 3
  • 13