2014-12-16 20 views
7

Sto cercando una metodologia per confrontare la differenza tra 2 righe nella stessa tabella. Da quello che ho trovato qui (How to get difference between two rows for a column field?) è quasi quello che volevo. Ho fatto il seguente codice:sql differenza di calcolo tra 2 righe

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 

Il mio problema è che il risultato che sto calcolando inizia sulla linea 1, invece di linea 2.

Di seguito il risultato sto ottenendo:

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 

mentre io vorrei che fosse così:

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 

Cosa c'è di sbagliato con il mio approccio?

+4

Perché non si può usare 'LAG' e le funzioni 'LEAD'? – Kermit

risposta

1

Non si limita il minimo allo stesso fondo e tipo di condivisione.

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 tt 
          where tt.ValueDate > hrc.ValueDate and 
            hrc.id_fund = tt.id_fund and hrc.id_ShareType = tt.id_ShareType 
          ) and 
      hrc.id_fund = hrn.id_fund and hrc.id_ShareType = hrn.id_ShareType ; 
+0

Hai ragione !!! Grazie mille :) – sa6

0

Prova questo:

update hrn 
set 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 
+0

Inoltre, limita il minimo per finanziare e condividere il tipo come suggerito da @Gordon Linoff – Russop

+0

In effetti sei corretto: dovrei limitare. Tuttavia, questo non risolve il mio problema. – sa6

+0

Lo fa. Se aggiorni hrn invece di hrc, i ritorni sono allineati con le date (il rendimento per il primo giorno è NULL). – Russop

0

Hi si può raggiungere questo obiettivo usando da CTE (Common Table Expression)

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) 

;With tbl as 

(Select Row_Number() OVER (Order by T.ValueDate) as RowNumber,* From #tmpTest T )SELECT Cur.*,(ISNULL(Cur.VarNAV,0) - ISNULL(Prv.VarNAV,0))/Prv.VarNAV as [Col Name] FROM tbl Cur LEFT OUTER JOIN tbl Prv ON Cur.RowNumber = Prv.RowNumber+1 ORDER BY Cur.ValueDate

+0

Evidenziando il tuo codice e premendo Ctrl + K lo formatterai, facilitando la lettura - vedi [come formattare qui] (http://meta.stackexchange.com/a/22189) – AHiggins

Problemi correlati