SELECT 'RFQ with available comparison data' as info;
SELECT r.rfq_id, r.rfq_no, r.status, COUNT(DISTINCT q.quotation_id) as quote_count
FROM biz_rfq r
JOIN biz_quotation q ON q.rfq_id = r.rfq_id AND q.status IN ('submitted','accepted')
GROUP BY r.rfq_id;

SELECT 'Comparison data detail for RFQ 4' as info;
SELECT ri.material_name, s.supplier_name, q.quote_no, q.status, qi.unit_price
FROM biz_rfq_item ri
JOIN biz_quotation_item qi ON qi.rfq_item_id = ri.item_id
JOIN biz_quotation q ON q.quotation_id = qi.quotation_id AND q.status IN ('submitted','accepted')
JOIN biz_supplier s ON s.supplier_id = q.supplier_id
WHERE ri.rfq_id = 4
ORDER BY ri.item_id, q.supplier_id;
