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
운영게시판
최근게시물
Oracle Q&A 39258 게시물 읽기
No. 39258
전년 대비 증감율 구하는 쿼리좀 도와주세요.
작성자
이장미(object81)
작성일
2012-01-27 14:38ⓒ
2012-01-27 14:44ⓜ
조회수
5,738

날짜                출고개수  
--------------     ----------
2005010200      2
2005010300      1
2005030200      6
2006040200      4
2006110200      2
2006110700      2
2007040200      4
2007111200      1
2007112200      2
2008010200      2
2008010300      1
2008030200      9

 

위와 같은 데이터를 가진 테이블이 있을때 다음과 같은 쿼리를 써서 년도별 합계를 구했는데요...


select substr(a.out_tm,1,4) as 기준년도, sum(a.out_cnt) as 출고합계
from data_table a
where
a.out_tm between '2005010100' AND '2008123123'
group by substr(a.out_tm,1,4) order by substr(a.out_tm,1,4) asc;

기준년도      출고합계
---------          -----------
2005                 9
2006                 8
2007                 7
2008                12

 

 

출고 합계 다음에 전년대비 증감수도 같이 나타 내고 싶습니다. 도움 부탁 드려요

 

기준년도      출고합계     증감수
---------        ----------       ------------
2005                 9                -
2006                 8                -1
2007                 7                -1
2008                12                5

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

with main as(
select '2005010200' as out_tm,       2 as out_cnt from dual union all
select '2005010300' as out_tm,       1 as out_cnt from dual union all
select '2005030200' as out_tm,       6 as out_cnt from dual union all
select '2006040200' as out_tm,       4 as out_cnt from dual union all
select '2006110200' as out_tm,       2 as out_cnt from dual union all
select '2006110700' as out_tm,       2 as out_cnt from dual union all
select '2007040200' as out_tm,       4 as out_cnt from dual union all
select '2007111200' as out_tm,       1 as out_cnt from dual union all
select '2007112200' as out_tm,       2 as out_cnt from dual union all
select '2008010200' as out_tm,       2 as out_cnt from dual union all
select '2008010300' as out_tm,       1 as out_cnt from dual union all
select '2008030200' as out_tm,       9 as out_cnt from dual
)
select substr(a.out_tm,1,4) as 기준년도
     , sum(a.out_cnt) as 출고합계
     , sum(a.out_cnt) - lag(sum(a.out_cnt)) over (order by substr(a.out_tm,1,4)) as 증감수
  from main a
 where a.out_tm between '2005010100' AND '2008123123'
 group by substr(a.out_tm,1,4) order by substr(a.out_tm,1,4) asc;
 

양승원(hanul072)님이 2012-01-27 15:00에 작성한 댓글입니다.
[Top]
No.
제목
작성자
작성일
조회
39261토드 옵션인지 먼지 한글 alias 관련 질문입니다. [2]
고영민
2012-02-01
5088
39260오라클 트리거 질문좀 할게요!! [2]
류하성
2012-02-01
3825
39259procedure 중간에 수행 관두기 [1]
나재호
2012-01-31
4154
39258전년 대비 증감율 구하는 쿼리좀 도와주세요. [1]
이장미
2012-01-27
5738
39257for 문안에 select [1]
나재호
2012-01-27
4196
39255자기자신 update [1]
미키
2012-01-26
3759
39254예측 데이터를 구하고 싶은데 잘 안되네요
박진경
2012-01-26
3540
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2024 DSN, All rights reserved.
작업시간: 0.017초, 이곳 서비스는
	PostgreSQL v16.2로 자료를 관리합니다