単純な集計で行う必要があります:
select id,
max(case when flag = 'I' then datetime end) indatetime,
max(case when flag = 'O' then datetime end) outdatetime
from t
group by id;
または、必要に応じて、ピボットを使用できます:
select id, [I] indatetime, [O] outdatetime
from t pivot (
max(datetime) for flag in ([I],[O])
) as p