Предполагая, что сальдо всегда увеличивается, вы можете просто найти сальдо в первый день, сальдо в последний день и рассчитать среднее значение (на основе количества дней):
;with minmax as ( -- subquery to get min / max data per user
select
username
,min(capturedate) as mincapturedate
,min(balance) as minbalance
,max(capturedate) as maxcapturedate
,max(balance) as maxbalance
from
[5171722] t
group by username
)
,averageincrease as ( -- subquery to calculate average daily increase
select
username
,datediff(day, mincapturedate, maxcapturedate) as numdays
,(maxbalance - minbalance) as totalincrease
,(maxbalance - minbalance) / datediff(day, mincapturedate, maxcapturedate) as
averagedailyincrease
from
minmax
)
-- pull results together, with highest average daily increase first
select
*
from
averageincrease
order by
averagedailyincrease desc
Поле averagedailyincrease
в конце содержит среднесуточное увеличение.