你可以使用
apply
:
select t.id, t.datetime, tt.Status,
sum(Count1) as Count1, sum(Count2) as Count2, min(rndata) as rndata
from table t cross apply (
select top 1 t1.Status
from table t1
where t1.id = t.id
order by t1.rndata
) tt
group by t.id, t.datetime;
但是,您也可以将其重新表示为via
窗口
功能:
select distinct id, datetime,
first_value(Status) over (partition by id order by rndata) as Status,
sum(count1) over (partition by id) as count1,
sum(count2) over (partition by id) as count2,
min(rndata) over (partition by id) as rndata
from table t;