I have a Product model that has many Skus, and each product can also have a Customization that includes being featured. Schema looks like this:
Product
| id | category_id | title | image |
Sku
| id | product_id | subtitle | price |
Customization
| id | product_id | title | featured |
I'm filtering/sorting the Product based on its category_id and the prices of its Skus like so:
SELECT id, title, image, prices
FROM (
SELECT p.id, p.title, p.image, array_agg(s.price) as prices
FROM "Products" p
JOIN "Skus" s
ON p.id = s.product_id
WHERE p.category_id in (1,2,3,4) AND s.price > 99
GROUP BY p.id
) x
ORDER BY id DESC;
I'd like to add an additional filter for Products whose Customizations have featured = true.
Is it possible to include another JOIN to do this, or is there a better approach?