-- Check price_history records per product (sample)
SELECT p.name, count(ph.id) as history_records, count(distinct po.feed_id) as num_shops
FROM products p
LEFT JOIN price_history ph ON ph.product_id = p.id
JOIN product_offers po ON po.product_id = p.id
GROUP BY p.id, p.name
HAVING count(ph.id) > 0
ORDER BY history_records DESC
LIMIT 10;

-- Check a product with 1 shop
SELECT p.name, ph.price, ph.recorded_at, ph.feed_id
FROM price_history ph
JOIN products p ON p.id = ph.product_id
JOIN product_offers po ON po.product_id = p.id
GROUP BY p.id, p.name, ph.id, ph.price, ph.recorded_at, ph.feed_id
HAVING count(distinct po.feed_id) = 1
LIMIT 10;
