database.sarang.net
UserID
Passwd
Database
DBMS
MySQL
PostgreSQL
Firebird
Oracle
Informix
Sybase
ㆍMS-SQL
DB2
Cache
CUBRID
LDAP
ALTIBASE
Tibero
DB 문서들
스터디
Community
공지사항
자유게시판
구인|구직
DSN 갤러리
도움주신분들
Admin
운영게시판
최근게시물
MS-SQL Q&A 4474 게시물 읽기
No. 4474
증가분 구하기...
작성자
조중규(biosensor)
작성일
2008-10-08 16:19ⓒ
2008-10-08 16:23ⓜ
조회수
5,082

샘플데이터는 다음과 같습니다.

stat_date

prj

g_1

g_2

g_3

g_4

2008-07-01

1

100

23

65

656

2008-07-01

2

12

3

6

54

2008-07-01

3

23

12

33

0

2008-07-02

1

123

44

89

767

2008-07-02

2

15

7

9

78

2008-07-02

3

34

33

44

0

2008-07-03

1

200

56

433

8976

2008-07-03

2

45

7

11

322

2008-07-03

3

66

67

55

0


보시면 stat_date와 prj 별로 누계 컬럼들이 4개(g_1, g_2, g_3, g_4) 있습니다.

stat_date는 약 6개월치가 있고요. prj는 1, 2, 3 이라는 데이터가 저장되어 있습니다. 물론 차후 추가될 수 있습니다.

각각의 누계 컬럼 g_1, g_2, g_3, g_4 은 매일의 누계를 배치로 돌려 작성하고 있습니다. (stat_date와 prj 별로 증가하고 있지요)

근데 문제는 각 stat_date와 prj 별로 증가분을 출력해내야 한다는 것입니다. 즉 같은 포맷의 증가분 g_1, g_2, g_3, g_4 를 새론 테이블로 만들어야 하죠.

순순한 증가분은 아래와 같이 되겠죠 (g_2에서 g_4는 생략했습니다)

stat_date

prj

g_1

g_2

g_3

g_4

2008-07-01

1

100

2008-07-01

2

12

2008-07-01

3

23

2008-07-02

1

23

2008-07-02

2

3

2008-07-02

3

11

2008-07-03

1

77

2008-07-03

2

30

2008-07-03

3

32


좋은 방법을 찾고져 질문 드립니다.

방법을 찾으면 배치프로세서로 매일 돌릴껀데 서버가 중지될수 있기때문에 -1 day는 고려하고 있지 않습니다. (서버 중지되면 당일 배치 결과분이 빠지기땜에...)

감사합니다.




이 글에 대한 댓글이 총 1건 있습니다.

--  집계자료라서 데이타량이 적으므로 그냥 간단하게 서브쿼리를 이용하셔도 될듯 하네요.

if object_id('res') is not null
drop table res
go

create table res
(idx int identity(1,1)  
, stat_date varchar(10)
, prj   int 
, g_1   int 
, g_2   int 
, g_3   int 
, g_4         int 

)
go


insert into res (  stat_date , prj , g_1 , g_2 , g_3 , g_4      )

select '2008-07-01' , 1   ,100 ,23 ,65 ,656  union
select '2008-07-01' ,2           ,12 ,3 ,6 ,54  union
select '2008-07-01' ,3 ,23 ,12 ,33 ,0  union
select '2008-07-02' ,1 ,123 ,44 ,89 ,767 union
select '2008-07-02' ,2 ,15 ,7 ,9 ,78 union
select '2008-07-02', 3 ,34 ,33 ,44 ,0 union
select '2008-07-03', 1 ,200 ,56 ,433 ,8976 union
select '2008-07-03', 2 ,45 ,7 ,11 ,322 union
select '2008-07-03', 3 ,66 ,67 ,55 ,0  

    
select   a.stat_date,
             a.prj ,
 a.g_1 -   isnull(( select g_1 from res where prj = a.prj  and stat_date =    (select max(stat_date) from res where stat_date < a.stat_date and prj = a.prj )),0) g_1 ,
 a.g_2 -   isnull(( select g_2 from res where prj = a.prj  and stat_date =    (select max(stat_date) from res where stat_date < a.stat_date and prj = a.prj )),0) g_2 ,
 a.g_3 -   isnull(( select g_3 from res where prj = a.prj  and stat_date =    (select max(stat_date) from res where stat_date < a.stat_date and prj = a.prj )),0) g_3 ,
 a.g_4  -   isnull(( select g_4 from res where prj = a.prj  and stat_date =    (select max(stat_date) from res where stat_date < a.stat_date and prj = a.prj )),0) g_4
   from  res a

 
zz님이 2008-10-09 02:29에 작성한 댓글입니다. Edit
[Top]
No.
제목
작성자
작성일
조회
4477테이블의 데이터 건수가 많아졌을땐 어떻게 해야 할까요?? [1]
아유카와
2008-10-11
4948
4476물리적인 저장구조에 대한 질문
서종범
2008-10-11
4977
4475ms-sql에서 쿼리 실행 전 인덱스 정보보려면... [1]
김정남
2008-10-09
5018
4474증가분 구하기... [1]
조중규
2008-10-08
5082
4473회원별 최근 주문내역을 뽑으려면.. [1]
제비우스
2008-10-08
5319
4472중복데이타 안불러오기.. [1]
음악소년
2008-10-08
5219
4471< MS -SQL에서 등록된 Record에서 가장 빠른 것을 찾은 다음에..> [3]
초보자
2008-10-08
5227
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2024 DSN, All rights reserved.
작업시간: 0.018초, 이곳 서비스는
	PostgreSQL v16.2로 자료를 관리합니다