테이블을 단순화하여
일자(datetime) 수금액(float) 두개의 필드만 있다고 가정하고
년월 누계액
2014.1 2014년1월1일부터 2014년1월말일까지수금액누계
2014.2 2014년1월1일부터 2014년2월말일까지수금액누계
...
2014.11 2014년1월1일부터 2014년11월말일까지수금액누계
2014.12 2014년1월1일부터 2014년12월말일까지수금액누계
2015.1 2015년1월1일부터 2015년1월말일까지수금액누계
2015.2 2015년1월1일부터 2015년2월말일까지수금액누계
...
2015.11 2015년1월1일부터 2015년11월말일까지수금액누계
2015.12 2015년1월1일부터 2015년12월말일까지수금액누계
결과가 위와 같은 뷰를 작성하고 싶습니다.전문적으로 공부하지 않고 어깨너머 배운지식이라 많이 딸립니다.
도움주시면 감사하겠습니다.
Comment 3
-
Terry
2015.03.24 13:41
-
길주석
2015.03.24 20:57
아! 감사합니다
언제나 이런 실력을 갖출까요.부럽고 또한번 감사드립니다
-
한태
2016.02.04 20:08
create table sal
(deptno int,
sal money)
insert into sal values(10, 1400)
insert into sal values(10, 700)
insert into sal values(10, 2000)
insert into sal values(20, 1600)
insert into sal values(20, 2800)
insert into sal values(30, 1000)
insert into sal values(30, 3000)
insert into sal values(30, 5000)
select * from sal
select a.deptno, (select SUM(sal) from sal b where b.deptno<=a.deptno)
from sal a
group by a.deptno
날짜 대신 부서번호로 테이블만들어서 테스트해봤습니다.
sql server 2012 부터는 sum()over(조건~~~) 이 되니까 보다 손쉽게 결과값을 찾아낼 수 있을거 같습니다.
--쿼리시작
With tbl (ymd,amt) As
(
Select '20140101',100500 Union All
Select '20140102',101500 Union All
Select '20140103',102500 Union All
Select '20140104',103500 Union All
Select '20140105',104500 Union All
Select '20140106',105500 Union All
Select '20140107',106500 Union All
Select '20140108',107500 Union All
Select '20140109',108500 Union All
Select '20140110',109500 Union All
Select '20140111',110500 Union All
Select '20140112',111500 Union All
Select '20140113',112500 Union All
Select '20140114',113500 Union All
Select '20140115',114500 Union All
Select '20140116',115500 Union All
Select '20140117',116500 Union All
Select '20140118',117500 Union All
Select '20140119',118500 Union All
Select '20140120',119500 Union All
Select '20140121',120500 Union All
Select '20140122',121500 Union All
Select '20140123',122500 Union All
Select '20140124',123500 Union All
Select '20140125',124500 Union All
Select '20140126',125500 Union All
Select '20140127',126500 Union All
Select '20140128',127500 Union All
Select '20140129',128500 Union All
Select '20140130',129500 Union All
Select '20140131',130500 Union All
Select '20140201',131500 Union All
Select '20140202',132500 Union All
Select '20140203',133500 Union All
Select '20140204',134500 Union All
Select '20140205',135500 Union All
Select '20140206',136500 Union All
Select '20140207',137500 Union All
Select '20140208',138500 Union All
Select '20140209',139500 Union All
Select '20140210',140500 Union All
Select '20140211',141500 Union All
Select '20140212',142500 Union All
Select '20140213',143500 Union All
Select '20140214',144500 Union All
Select '20140215',145500 Union All
Select '20140216',146500 Union All
Select '20140217',147500 Union All
Select '20140218',148500 Union All
Select '20140219',149500 Union All
Select '20140220',150500 Union All
Select '20140221',151500 Union All
Select '20140222',152500 Union All
Select '20140223',153500 Union All
Select '20140224',154500 Union All
Select '20140225',155500 Union All
Select '20140226',156500 Union All
Select '20140227',157500 Union All
Select '20140228',158500 Union All
Select '20140305',159500 Union All
Select '20140320',160500 Union All
Select '20140331',161500 Union All
Select '20140422',162500 Union All
Select '20140428',163500 Union All
Select '20140507',164500 Union All
Select '20140531',165500 Union All
Select '20140605',166500 Union All
Select '20140630',167500 Union All
Select '20140703',168500 Union All
Select '20140822',169500 Union All
Select '20140910',170500 Union All
Select '20141008',171500 Union All
Select '20141101',172500 Union All
Select '20141108',173500 Union All
Select '20141202',174500 Union All
Select '20141209',175500 Union All
Select '20150103',176500 Union All
Select '20150113',177500 Union All
Select '20150201',178500 Union All
Select '20150217',179500 Union All
Select '20150311',180500 Union All
Select '20150324',181500
)
Select
Left(a.ymd,6) As yymm
, Sum(a.amt )
+ IsNull(
(
Select Sum(b.amt)
From tbl As b
Where Left(b.ymd,4) = Left(Left(a.ymd,6),4)
And Left(b.ymd,6) < Left(a.ymd,6)
)
,0
)
As amt
From tbl As a
Group By Left(a.ymd,6)
Order By Left(a.ymd,6) Asc
--쿼리끝