select ref, count(distinct id) from table group by ref;
Dette vil give dig en optælling af distinkt id ved ref.
select ref, count(*) from table group by ref;
Dette vil give dig antallet af poster pr. ref.
EDIT:
Prøv dette for at få det output, du ønsker.
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
Eksempel på SQLFiddle:http://sqlfiddle.com/#!9/2b93c/2