Skip to content

Instantly share code, notes, and snippets.

@Bwilliamson55
Created July 26, 2022 21:26
Show Gist options
  • Save Bwilliamson55/cee08d114e03a088587a1bb2676f9e63 to your computer and use it in GitHub Desktop.
Save Bwilliamson55/cee08d114e03a088587a1bb2676f9e63 to your computer and use it in GitHub Desktop.
Magento 2 CE sku_and_attribute query builder
SELECT
GROUP_CONCAT(
CONCAT(
' MAX(IF(attribute_code = ''',
t.attribute_code,
''', value, NULL)) AS ',
t.attribute_code
)
) INTO @PivotQuery
FROM
(SELECT
sku_code_value.attribute_code as attribute_code
FROM
(SELECT ce.entity_id,
ce.sku,
ea.attribute_code,
CASE ea.backend_type
WHEN 'varchar' THEN ce_varchar.value
WHEN 'int' THEN ce_int.value
WHEN 'text' THEN ce_text.value
WHEN 'decimal' THEN ce_decimal.value
WHEN 'datetime' THEN ce_datetime.value
ELSE ea.backend_type
END AS value
FROM (select cpe.sku, eas.entity_type_id, cpe.entity_id
FROM catalog_product_entity AS cpe,
eav_attribute_set AS eas
WHERE cpe.attribute_set_id = eas.attribute_set_id) AS ce
LEFT JOIN eav_attribute AS ea
ON ce.entity_type_id = ea.entity_type_id
LEFT JOIN catalog_product_entity_varchar AS ce_varchar
ON ce.entity_id = ce_varchar.entity_id
AND ea.attribute_id = ce_varchar.attribute_id
AND ea.backend_type = 'varchar'
LEFT JOIN catalog_product_entity_int AS ce_int
ON ce.entity_id = ce_int.entity_id
AND ea.attribute_id = ce_int.attribute_id
AND ea.backend_type = 'int'
LEFT JOIN catalog_product_entity_text AS ce_text
ON ce.entity_id = ce_text.entity_id
AND ea.attribute_id = ce_text.attribute_id
AND ea.backend_type = 'text'
LEFT JOIN catalog_product_entity_decimal AS ce_decimal
ON ce.entity_id = ce_decimal.entity_id
AND ea.attribute_id = ce_decimal.attribute_id
AND ea.backend_type = 'decimal'
LEFT JOIN catalog_product_entity_datetime AS ce_datetime
ON ce.entity_id = ce_datetime.entity_id
AND ea.attribute_id = ce_datetime.attribute_id
AND ea.backend_type = 'datetime') sku_code_value
GROUP BY
sku_code_value.attribute_code) t;
SET @PivotQuery = CONCAT('SELECT sku,', @PivotQuery, ' FROM (SELECT ce.entity_id,
ce.sku,
ea.attribute_code,
CASE ea.backend_type
WHEN ''varchar'' THEN ce_varchar.value
WHEN ''int'' THEN ce_int.value
WHEN ''text'' THEN ce_text.value
WHEN ''decimal'' THEN ce_decimal.value
WHEN ''datetime'' THEN ce_datetime.value
ELSE ea.backend_type
END AS value
FROM (select cpe.sku, eas.entity_type_id, cpe.entity_id
FROM catalog_product_entity AS cpe,
eav_attribute_set AS eas
WHERE cpe.attribute_set_id = eas.attribute_set_id) AS ce
LEFT JOIN eav_attribute AS ea
ON ce.entity_type_id = ea.entity_type_id
LEFT JOIN catalog_product_entity_varchar AS ce_varchar
ON ce.entity_id = ce_varchar.entity_id
AND ea.attribute_id = ce_varchar.attribute_id
AND ea.backend_type = ''varchar''
LEFT JOIN catalog_product_entity_int AS ce_int
ON ce.entity_id = ce_int.entity_id
AND ea.attribute_id = ce_int.attribute_id
AND ea.backend_type = ''int''
LEFT JOIN catalog_product_entity_text AS ce_text
ON ce.entity_id = ce_text.entity_id
AND ea.attribute_id = ce_text.attribute_id
AND ea.backend_type = ''text''
LEFT JOIN catalog_product_entity_decimal AS ce_decimal
ON ce.entity_id = ce_decimal.entity_id
AND ea.attribute_id = ce_decimal.attribute_id
AND ea.backend_type = ''decimal''
LEFT JOIN catalog_product_entity_datetime AS ce_datetime
ON ce.entity_id = ce_datetime.entity_id
AND ea.attribute_id = ce_datetime.attribute_id
AND ea.backend_type = ''datetime'') GROUP BY sku');
select @PivotQuery
@Bwilliamson55
Copy link
Author

This will create a select statement for all of your product attributes, to be used as a flat-table type query.
I noticed recently that if your attribute is varchar/select this doesn't build in the correct sub-query and I'll fix that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment