select ref, count(distinct id) from table group by ref;
Detta ger dig en räkning av distinkt id genom ref.
select ref, count(*) from table group by ref;
Detta ger dig antalet poster per ref.
EDIT:
Prova detta för att få den utdata du önskar.
select t.*, m.counter
from table t
join (
select ref, count(distinct id) as counter
from table group by ref
) m on t.ref = m.ref
Exempel på SQLFiddle:http://sqlfiddle.com/#!9/2b93c/2