데이터 누계 질문드립니다.
일별 계산은 되는데... 월별, 분기별, 연도별 누계를 계산하려니 잘 안됩니다.

use tempdb
go
if object_id ('dbo.#t') is not null
drop table dbo.#t
go

print 'begin-------------------------'
--iDate: primary key
--iVal: 매일 매일 연속 누적됨
create table #t(idate smalldatetime, iVal int)
insert #t(idate,ival) values('2018-12-31',90)
insert #t(idate,ival) values('2019-01-01',100)
insert #t(idate,ival) values('2019-01-02',110)
insert #t(idate,ival) values('2019-01-03',120)
insert #t(idate,ival) values('2019-01-04',130)
insert #t(idate,ival) values('2019-01-05',140)
insert #t(idate,ival) values('2019-01-06',250)
insert #t(idate,ival) values('2019-02-10',400)
insert #t(idate,ival) values('2019-02-16',410)

go

print 'result-------------------------'
select x.idate, x.ival as to_day, y.ival as be_day, x.ival-y.ival as day_val
from
(select * , (select count(*) from #t as b  where b.idate<a.idate) as cnt1
from #t as a) as x left join 
(select * , (select count(*)+1 from #t as b  where b.idate<a.idate) as cnt1
from #t as a) as y on x.cnt1=y.cnt1
where x.idate between '2019-01-01' and '2019-12-31'


-- 결과값:  계산 결과는  금일,     전일,       일별 값,       (일별누계,  월별누계, 분기별 누계, 연도별 누계) 컬럼을 추가하고 싶습니다.
--idate                   to_day      be_day      day_val
----------------------- ----------- ----------- -----------
2019-01-01 00:00:00     100         90          10
2019-01-02 00:00:00     110         100         10
2019-01-03 00:00:00     120         110         10
2019-01-04 00:00:00     130         120         10
2019-01-05 00:00:00     140         130         10
2019-01-06 00:00:00     250         140         110
2019-02-10 00:00:00     400         250         150
2019-02-16 00:00:00     410         400         10

(8개 행이 영향을 받음)

go

print '누계 계산 Select 문 오류가 발생합니다'
select x.idate, x.ival as to_day, y.ival as be_day, x.ival-y.ival as day_total
,(select sum(ival) 
  from #t 
  where  idate between '2019-01-01' and '2019-12-31' 
and idate)<x.idate) as day_total
,(select sum(ival) 
  from #t 
  where  idate between '2019-01-01' and '2019-12-31' 
and datepart(m,idate)=datepart(m,x.idate)) as mon_total
,(select sum(ival) 
  from #t 
  where  idate between '2019-01-01' and '2019-12-31' 
and datepart(q,idate)=datepart(q,x.idate)) as qq_total
from
(select * , (select count(*) from #t as b  where b.idate<a.idate) as cnt1
from #t as a) as x left join 
(select * , (select count(*)+1 from #t as b  where b.idate<a.idate) as cnt1
from #t as a) as y on x.cnt1=y.cnt1
where x.idate between '2019-01-01' and '2019-12-31'
go



조언을 바랍니다.


좋은 주말 되시구요~~