select [DateTime],num=isnull((select count(*) from 表 where [DateTime]<a.[DateTime]),0)
from(select distinct [DateTime]=convert(char(10),[DateTime],120) from 表)a
from(select distinct [DateTime]=convert(char(10),[DateTime],120) from 表)a
,num=sum(select 1 form tb b where where b.datetime<a.datetime
from tb a group by covnert(varchar(10),[DateTime],120)