Dies liefert die Anzahl unterschiedlicher IDs pro Referenz:
select ref, count(distinct id) from table group by ref;Und dies die Gesamtzahl der Datensätze pro Referenz:
select ref, count(*) from table group by ref;Ergebnis inklusive Gegenwert pro Zeile
EDIT: Um die gewünschte Ausgabe zu erhalten, versuchen Sie dies:
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.refBeispiel auf SQLFiddle: https://sqlfiddle.com/#!9/2b93c/2