I’ve got the following reference data.
STORE_CODE SUPPLIER_CODE
------ ------
ASDA A3
ASDA A2
TESCO A2
TESCO A4
ASDA A4
ASDA A4
ASDA A4
ASDA A4
WAITROSE A3
ASDA A4
WAITROSE A3
ASDA A3
ASDA A3
ASDA A4
SAINSBURY A2
SAINSBURY A2
SAINSBURY A6
TESCO A6
ASDA A3
TESCO A3
TESCO A3
SAINSBURY A3
WAITROSE A5
WAITROSE A5
WAITROSE A5
WAITROSE A4
ALDI A1
ALDI A1
ALDI A1
ALDI A1
ALDI A1
ALDI A1
ALDI A1
ALDI A1
What I want to do, is to display a list of suppliers, who have at least 4 unique stores associated to them.
So far, this is what I’ve conjugated :
SELECT
distinct stoc.supplier_code as SUPPLIER_CODE
FROM
MYTABLE stoc
WHERE
(select count(distinct a.store_code) from mytable a where a.supplier_code = stoc.supplier_code) >= 4
;
Is there anyway to optimize my select statement? The where clause looks rather messy and I was wondering if I can improve it by using GROUP BY or HAVING?
Many thanks
Try this: