sybase ase - sql compute difference between 2 rows -
i'm looking methodology compare difference between 2 rows in same table. found here (how difference between 2 rows column field?) it's wanted. have done following code:
create table #tmptest ( id_fund int null, id_sharetype int null, valuedate datetime null, varnav float null, fundperf float null, ) insert #tmptest(id_fund, id_sharetype, valuedate, varnav) values(1,1,'20140101',100) insert #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 #tmptest hrc left join #tmptest hrn on hrn.valuedate = (select min(valuedate) #tmptest valuedate > hrc.valuedate) , hrc.id_fund = hrn.id_fund , hrc.id_sharetype = hrn.id_sharetype
my issue result i'm computing starts on line 1 instead of line 2.
hereunder result i'm obtaining:
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
whereas i'd way:
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
what's wrong approach?
you not restricting minimum same fund , share type.
update #tmptest set hrc.fundperf = (isnull(hrn.varnav, 0) - hrc.varnav)/hrc.varnav #tmptest hrc left join #tmptest hrn on hrn.valuedate = (select min(valuedate) #tmptest tt tt.valuedate > hrc.valuedate , hrc.id_fund = tt.id_fund , hrc.id_sharetype = tt.id_sharetype ) , hrc.id_fund = hrn.id_fund , hrc.id_sharetype = hrn.id_sharetype ;
Comments
Post a Comment