내가 여기에 내가 제대로 시나리오를 해석하는 경우 확실하지 오전하지만, 내가 무슨 짓을 :
(나는 함수에 입력을 모방하기 위해 몇 가지 변수를 만든 다음 재귀 공통 테이블 표현식을 구성하는 것, 시작하려면
create variable OpenedWith decimal default 5000
;
create variable Factor decimal(2, 1) default 0.1
;
with
t (adjustment) as (values(100), (200), (300))
, ordRows (rn, Adjustment) as
(select row_number() over(), Adjustment from t)
, addRows (rn, Opening, Shift, Adjustment, closing) as
(select rn, OpenedWith, OpenedWith*Factor , Adjustment
, (OpenedWith + (OpenedWith*Factor) + Adjustment)
from ordRows
where rn = 1
union all
select b.rn, a.Closing, a.Closing * Factor , b.Adjustment
, (a.Closing + (a.Closing * Factor) + b.Adjustment)
from addRows a
join ordRows b
on a.rn = (b.rn - 1)
)
select int(rn) as rn, int(opening) as opening
, int(shift) as shift, adjustment
, int(closing) as closing
from addRows
,321 : RCTE)은이 변수로부터 얻어진 데이터를 사용하여 보고서를 생성 할
RN OPENING SHIFT ADJUSTMENT CLOSING
1 5,000 500 100 5,600
2 5,600 560 200 6,360
3 6,360 636 300 7,296
이제 T라는 이름의 테이블에있는 데이터에 대해 작동하는 사용자 정의 테이블 함수 (UDTF)에 위의 스크립트 변수 작품 및 쿼리를 수정할 수 : 0 다음 위의 질의에서 보고서입니다
create function shifted_vals
(OpenedWith decimal(5)
, Factor decimal(3, 2)
)
returns table
(Opening int
, Shift int
, Adjustment int
, Closing int
)
return
with
ordRows (rn, Adjustment) as
(select row_number() over(), Adjustment from t)
, addRows (rn, Opening, Shift, Adjustment, closing) as
(select rn, OpenedWith, OpenedWith*Factor , Adjustment
, (OpenedWith + (OpenedWith*Factor) + Adjustment)
from ordRows
where rn = 1
union all
select b.rn, a.Closing, a.Closing * Factor , b.Adjustment
, (a.Closing + (a.Closing * Factor) + b.Adjustment)
from addRows a
join ordRows b
on a.rn = (b.rn - 1)
)
select opening, shift, adjustment, closing
from addRows
order by rn
지금 인자로서 주목 개구 값 및 계수와 UDTF을 호출; 즉, 더 이상 생성 된 변수에 의존하지 않고 입력 매개 변수를 통해 얻은 값 대신 다음을 입력하십시오.
select t.*
from table(shifted_vals(5000, 0.1)) as t
; -- results as report, follows:
OPENING SHIFT ADJUSTMENT CLOSING
5,000 500 100 5,600
5,600 560 200 6,360
6,360 636 300 7,296
_ 이전 행 _을 판별 할 수 있도록 테이블 T의 데이터 정렬을 정의합니다. 아마 특별한 순서없이 row_number() over()? 또한 OP에서 2)는 Opening + Shift + Closing으로 정의되지만, 결과에 따라 표현식은 Opening + Shift + Adjustment 여야합니다. – CRPence