동일한 테이블의 2 행 간의 차이를 비교하는 방법론을 찾고 있습니다. 내가 여기에서 발견했던 것에서 (How to get difference between two rows for a column field?) 거의 내가 원했던 것이다.sql 2 행 간의 차이 계산
create table #tmpTest
(
id_fund int null,
id_ShareType int null,
ValueDate datetime null,
VarNAV float null,
FundPerf float null,
)
insert into #tmpTest(id_fund, id_ShareType, ValueDate, VarNAV)
values(1,1,'20140101',100)
insert into #tmpTest(id_fund, id_ShareType, ValueDate, VarNAV)
values(1,1,'20140102',20)
update #tmpTest
set hrc.FundPerf = (isnull(hrn.VarNAV, 0) - hrc.VarNAV)/hrc.VarNAV
from #tmpTest hrc
left join #tmpTest hrn on hrn.ValueDate = (select min(ValueDate) from #tmpTest where ValueDate > hrc.ValueDate)
and hrc.id_fund = hrn.id_fund and hrc.id_ShareType = hrn.id_ShareType
내 문제는 내가 그 결과 본 계약 대신 라인 2.
의 라인 1에 시작을 계산하고있어 결과가 내가 얻을 수있어 것을 : 나는 다음과 같은 코드를 수행 한
id_fund id_ShareType ValueDate VarNAV FundPerf
------- ------------ ------------------- ------- -----------------------------
1 1 2014-01-01 00:00:00 100 -0.8
1 1 2014-01-02 00:00:00 20 -1
내가 그렇게 싶습니다 반면 :
id_fund id_ShareType ValueDate VarNAV FundPerf
------- ------------ ------------------- ------- -----------------------------
1 1 2014-01-01 00:00:00 100 -1
1 1 2014-01-02 00:00:00 20 -0.8
내 접근 방식에 어떤 문제가 있습니까?
'LAG'하여 사용하여 얻을 수 있습니다 'LEAD'는 기능을합니까? – Kermit