sql - Percentage difference between numbers in two columns -
my sql experience minimal please go easy on me here. have table tblforex
, i'm trying create query looks @ 1 particular column lastsalesratechangedate
, forexrate
.
basically want query check lastsalesratechangedate
, pull forexrate
on same line (obviously in forexrate
column), need check see if there +/- 5% change since last time lastsalesratechangedate
changed. hope makes sense, tried explain possible.
i believe need create 'subquery' @ lastsalesratechangedate
, pull forex
rate date, don't know how go this.
i should add being done in access (sql)
sample data, here table looks like:
| basecur | forcur | forexrate | lastsalesratechangedate | usd | brl | 1.718 | 12/9/2008 | usd | brl | 1.65 | 11/8/2008
so need query @ lastsalesratechangedate
column, check see if date has changed, if take forexrate
value , give percentage difference of forexrate
value since last record.
so final result like
"basecur" "forcur" "percentage change since last sales rate change" usd brl x%
gordon's answer pointed in right direction:
select t2.*, (select top 1 t.forexrate tblforex t t.basecur=t2.basecur , t.forcur=t2.forcur , t.lastsalesratechangedate<t2.lastsalesratechangedate order t.lastsalesratechangedate desc, t.forexrate desc ) previousrate, [forexrate]/[previousrate]-1 changeratio tblforex t2;
access gives errors top 1
in subquery causes "ties". broke ties , therefore removed error adding item order by
clause. ratio display percentage, switch design view , change properties of column accordingly.
Comments
Post a Comment