;WITH CTE as (select row_number() over (order by date) as rn,Date,UserCountToday,UserCount7days from tbl5)
,CTERec as (
select rn,Date,UserCountToday ,UserCountToday as UserCount7days from CTE where rn=1
union all
select c.rn,c.Date,c.UserCountToday ,c.UserCountToday+c1.UserCount7days as UserCount7days from CTERec c1 inner join CTE c
on c.rn=c1.rn+1
)
select Date,UserCountToday,CASE WHEN rn<=7 then UserCount7days else (UserCount7days-(select UserCount7days from CTERec where rn=c.rn-7)) end asUserCount7days from CTERec c
抱歉,我不udnerstand你的問題 –