select ID,AA = (select top 1 AA from test where id = a.id)
,BB = (select top 1 BB from test where id = a.id)
from test a
group by ID--Result
1 123 456
2 50 664
3 89 89
4 40 4242