1

최대한 효율적으로이를 해결하기 위해 아이디어 (가급적 CTE)를 알려주십시오. CTE 또는 기타 효율적인 계산을 통한 재귀 채우기 계산

enter image description here

그럼

도시 된 표에서 빨간색 "값"열에서의 셀은 알려진 값 상기 강조 채소 옆에 나타내는 식으로 계산되는 값이다이다. 이것이 CTE와 함께 가능한지 알고 싶습니다.

마지막으로 알려진 값과 해당 간격과 같습니다. 다음의 알려진 값 및 각각의 간격; 및 상기 값이 계산되는 간격; 모두는 그 다음 인턴이 다음에 알려지지 않은 값을 위해 똑같은 방식으로 사용될 값을 찾는 데 사용됩니다. 여기

+0

선형 보간이라고 생각합니다. – Paciv

+0

@podiluska .NET에서이 작업을 수행하는 루프가 있으며 동일한 작업을 수행하는 모든 수식이 포함 된 Excel 템플릿도 있습니다. 왜냐하면 간격이 일정하고 특정 간격으로 각각의 값이 알려지기 때문에 Excel은 쉽게 할 수 있다고 생각했습니다. 그러나 접근 방식과 같은 절차를 갖는 것은 내가 시도하고 있지만 가능한 경우 루프가없는 것입니다. 또한 Excel에서 매월 새 행을 추가 할 때 수식을 추가해야하는 문제가 있습니다. – TiredMaster

+0

@Paciv 알 수없는 값은 계산되지만 추정되지는 않습니다. 또한 솔루션은 보간에 대한 계산상의 오버 헤드를 갖는 것보다 훨씬 더 간단 할 수 있다고 생각합니다. 그러나 그것이 무엇을 찾는다 고 생각한다면, 나에게 리드를주십시오. 감사. – TiredMaster

답변

1

내가 (storevalue이 예에서 시작 테이블입니다)

with knownvalues as (
select store, shipNtrvl,value 
from storevalue where Value is not null 
), valueranges as 
(
select 
k.store, 
k.ShipNtrvl as lowrange, 
MIN(s.ShipNtrvl) as highrange, 
(select value from storevalue where store = k.store and ShipNtrvl = MIN(s.shipNtrvl))- 
(select value from storevalue where store = k.store and ShipNtrvl = k.ShipNtrvl) as  term1, 
MIN(s.ShipNtrvl) - k.ShipNtrvl as term2,min(k.Value) as lowval 
from knownvalues k 
join storevalue s on s.Value is not null and s.store= k.store and s.ShipNtrvl >  k.ShipNtrvl 
group by k.store, k.shipntrvl 
) 
select s.store,s.ShipNtrvl,v.term1/v.term2*(s.ShipNtrvl-v.lowrange)+ v.lowval as value 
from storevalue s join valueranges v on v.store = s.store and s.ShipNtrvl between v.lowrange and v.highrange 
where s.Value is null 
union 
select * from storevalue where value is not null 

그냥 테이블에 값을 작성하는 업데이 트에 선택을 변경 해낸 것입니다.

2

다음은 해결책입니다.

희망이 있습니다. :)

;with testdata(store,shipntrvl,value) 
as 
(
select 'abc', 1, 0.56 
union all 
select 'abc', 5, null 
union all 
select 'abc', 10, 0.63 
union all 
select 'abc', 15, null 
union all 
select 'abc', 20, null 
union all 
select 'abc', 25, null 
union all 
select 'abc', 30, 0.96 
union all 
select 'xyz', 1, 0.36 
union all 
select 'xyz', 5, 0.38 
union all 
select 'xyz', 10, null 
union all 
select 'xyz', 15, 0.46 
union all 
select 'xyz', 20, null 
union all 
select 'xyz', 25, null 
union all 
select 'xyz', 30, 0.91 
) 
,calc 
as 
(
select * 
     ,ROW_NUMBER() OVER(partition by store order by shipntrvl) as row_no 
from testdata 
) 
,extra 
as 
(
select * 
     ,(select top 1 row_no 
      from  calc c2 
      where  c2.row_no < c1.row_no 
      and  c1.value is null 
      and  c2.value is not null 
      and  c1.store = c2.store 
      order by c2.row_no desc) as prev_nr 
     ,(select top 1 row_no 
      from  calc c2 
      where  c2.row_no > c1.row_no 
      and  c1.value is null 
      and  c2.value is not null 
      and  c1.store = c2.store 
      order by c2.row_no asc) as next_nr 
from calc c1 
) 

select c.store 
     ,c.shipntrvl 
     ,c.value 
     ,isnull(c.value, 
      (cnext.value-cprev.value)/ 
      (cnext.shipntrvl-cprev.shipntrvl)* 
      (c.shipntrvl-cprev.shipntrvl)+cprev.value 
     ) as calculated_value 
from calc c 
join extra 
    on extra.row_no = c.row_no 
    and extra.store = c.store 
join calc cnext 
    on cnext.row_no = case when c.value is null 
          then extra.next_nr 
          else c.row_no 
          end 
    and c.store = cnext.store 
join calc cprev 
    on cprev.row_no = case when c.value is null 
          then extra.prev_nr 
          else c.row_no 
          end 
    and c.store = cprev.store 
+0

나는 당신의 제안을 좋아한다. 그것은 나의 것보다 우아하다. 잘 했어! +1 –

+0

@ 윌리암 토드 잘츠 만 : 고마워! :) – Johan