/********************************************************************************************
-- Title : [2k8] 테이블 전체 ROW에서 중간 ROW의 값 구하기
-- Key word : 중간값 로우 rows row tct 지수 중위수 clt 지수
********************************************************************************************/
-- 전체 로우수가 홀수일 때는 중간값을, 짝수일 때는 가운데 2개 값의 평균을 추출 Like below.
use tempdb
go
-- 홀수 ROW 테이블 생성
create table #ttt_odd
( seq int not null
, val numeric(5,1) not null
);
create table #ttt_odd
( seq int not null
, val numeric(5,1) not null
);
-- 짝수 ROW 테이블 생성
create table #ttt_even
( seq int not null
, val numeric(5,1) not null
);
create table #ttt_even
( seq int not null
, val numeric(5,1) not null
);
-- 홀수 ROW 데이터 입력
insert into #ttt_odd
select 1, 3.4 union all
select 2, 4.2 union all
select 3, 5.7;
insert into #ttt_odd
select 1, 3.4 union all
select 2, 4.2 union all
select 3, 5.7;
-- 짝수 ROW 데이터 입력
insert into #ttt_even
select 1, 3.4 union all
select 2, 4.2 union all
select 3, 5.7 union all
select 4, 7.3;
insert into #ttt_even
select 1, 3.4 union all
select 2, 4.2 union all
select 3, 5.7 union all
select 4, 7.3;
-- 홀수 ROW에서 중간 ROW의 val 추출
declare @max_seq int;
declare @max_seq int;
select @max_seq = max(seq)
from #ttt_odd;
from #ttt_odd;
select a.val
from
( select seq, val, seq*10 "seq2"
from #ttt_odd
where seq*10 = ((@max_seq+1)*10)/2
) a;
from
( select seq, val, seq*10 "seq2"
from #ttt_odd
where seq*10 = ((@max_seq+1)*10)/2
) a;
-- 짝수 ROW에서 중간 ROW의 avg(val) 추출
declare @max_seq int;
declare @max_seq int;
select @max_seq = max(seq)
from #ttt_even;
from #ttt_even;
select avg(a.val) "val"
from
( select seq, val, seq*10 "seq2"
from #ttt_even
where seq*10 between @max_seq*10/2 and ((@max_seq*10)/2)+10
) a;
from
( select seq, val, seq*10 "seq2"
from #ttt_even
where seq*10 between @max_seq*10/2 and ((@max_seq*10)/2)+10
) a;