이런식으로 소계를 구하려면 어떻게 해야하는지요? 특정 행의 갯수마다 소계를 구하고 싶거든요
답변좀 꼭 부탁드립니다.
by finecomp
[2011.01.09 16:08:27]
없다면, 필요한 컬럼을 조회시 가공하면 되겠죠?
3row씩이라면, CEIL(ROWNUM/3) AS gbn
정도 만들어서 GROUP BY ROLLUP(name, gbn) 해 보세요.
ps) 혹시, GROUP BY, ROLLUP의 방법은 좌측메뉴 Oracle 강좌에서 확인하세요.
by camela
[2011.01.09 18:37:08]
finecomp 님이 말씀하신대로 하면되는데,
GROUP BY ROLLUP(name, gbn) 으로 하시면 안되고
GROUP BY ROLLUP(gbn,name) 으로 하시면 됩니다.
아래처럼 하면 됩니다.
롤업 이용하고, 소계만 보이게 전체합계는 제외하였습니다.
with tb as
(
select 'A' name, 1 point from dual union all
select 'B' name, 1 point from dual union all
select 'C' name, 1 point from dual union all
select 'D' name, 2 point from dual union all
select 'E' name, 2 point from dual union all
select 'F' name, 2 point from dual
)
select decode(grouping_id(name),1,'소계'
,name) as name
, sum(point) as point
from tb
group by rollup (ceil(rownum/3),name)
having grouping_id(ceil(rownum/3),name) <> 3
by 마농
[2011.01.10 08:15:19]
다음과 같이 하면 Having 절이 필요 없죠.
GROUP BY CEIL(ROWNUM/3), ROLLUP(name)
by camela
[2011.01.10 08:50:40]
흠...그렇네요.
name 만 롤업하면 되겠네요.^^
댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.