SQLite Forum

More math help and what I've learned
Login
Hello, you will realize that only the first row in your table has no value for the previous_transId. But then it is easy to not only show this in the table but also use it to select between the different formulas. If there is a previous then use the formula using it and **else** use the formula for the first record. I think the **case** will be your future helper!

~~~
SELECT *,
	(GrowthPool-GCOVAW)/1000000-EstGrowth as Diff
FROM (
  SELECT
	TransactionId,
	GrowthPool,

        TransactionId               as current_transId,

        lag(TransactionId,1) OVER w as previous_transId,

        case when lag(TransactionId,1) OVER w
             then ((TransactionId) -  (lag(TransactionId,1,1) OVER w))*0.003*250
             else                                       TransactionId *0.003*250
             end as EstGrowth,

	lag(GrowthCarry) OVER
	w Previous_GCOV, GrowthCarry as Current_GCOV,
	(lag(GrowthCarry) OVER w) as GCOVAW

  FROM CalculateGrowth
  WINDOW w AS (PARTITION BY PIG ORDER BY PIG)
) e
WHERE EstGrowth IS NOT NULL;
~~~