Modifica:ok, ora hai modificato la domanda in modo tale che questa risposta sembri completamente irrilevante ... sospiro ... la lascerò nel caso ti aiuti a iniziare. Saluti.
La soluzione più semplice date le tue specifiche:
select teacherid
from mytable
group by teacherid;
Se hai bisogno di altre informazioni oltre al teacherid
:
select teacherid, ...other cols...
from (select teacherid, ...other cols...
row_number() over (
partition by teacherid
order by classid /* or class as per edit */) as row_num
from mytable) my_derived_table
where my_derived_table.row_num = 1;
Attenzione:non ho un'installazione di SQL-Server a portata di mano su cui testare, quindi la sintassi potrebbe non essere esattamente corretta; ma è vicino.