Examples of the Attribute_Code column are: Size, Colour, Expiry_Date, etc.
I want to flatten each attribute I need so that it’s presented as follows in different columns:
Product_ID||Size||Colour
123456||20||Black
In SQL form, this is what I have:
SELECT
( SELECT ATTR.ATTRIBUTE_VALUE FROM PRODUCT_ATTRIBUTE ATTR WHERE ATTR.ATTRIBUTE_CODE = 'SIZE'
AND ATTR.ATTRIBUTE_ID = PROD.ATTRIBUTE_ID ) AS SIZE,
( SELECT ATTR.ATTRIBUTE_VALUE FROM PRODUCT_ATTRIBUTE ATTR WHERE ATTR.ATTRIBUTE_CODE = 'COLOUR'
AND ATTR.ATTRIBUTE_ID = PROD.ATTRIBUTE_ID ) AS COLOUR
FROM PRODUCT PROD
Any ideas on how I’d go about implementing this in my universe?
Note: There is already a pivot solution that flattens this at the DB level using similar code to the above,
however I’m exploring other alternatives.
Well, you can always create a dervied table using pretty much exactly the SQL you have up there.
You could also turn your Product table into a pseudo fact table, by creating a series of derived tables. Create a “Prod_Size” derived table from the Product_Attribute table where Attribute_Code = “size”. Join that derived table back to the Product table, and now the Attribute_Value column becomes simply the Size value. Rinse, lather, and repeat for all the other attributes.
(Note, either of these methods requires the possible values for Attribute_Code to remain pretty static. Any new code added will require you to go either add a new line to the SQL of the first one, or add a new derived table to the second one.)