Percentage difference between numbers in two fields

natonstan

New Member
Joined
Mar 2, 2017
Messages
8
My SQL experience is fairly minimal so please go easy on me here. I have a table named tblForEx and I'm trying to create a query that looks at one particular filed titled LastSalesRateChangeDate and also ForExRate.

Basically what I want to do is for the query to check that LastSalesRateChangeDate and then pull the ForExRate that is on the same line (Obviously in the ForExRate field), then I need to check to see if there is a +/- 5% change since the last time the LastSalesRateChangeDate changed. I hope this makes sense, I tried to explain it as clearly as possible.
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
I've managed to get this code from someone, I can't work out why it won't work, am I right in thinking I need to replace the 't' with my table tblForEx?

Code:
select t.*, ( (t.ForExRate / tprev.ForExRate) - 1) as change_ratio
from (select t.*,
             (select top (1) t2.LastSalesRateChangeDate
              from tblForEx as t2
              where t2.BaseCur = t.BaseCur and t2.ForCur = t.ForCur
                    t2.LastSalesRateChangeDate < t.LastSalesRateChangeDate
              order by t2.LastSalesRateChangeDate desc
             ) as prev_LastSalesRateChangeDate
      from t
     ) as t inner join
     tblForEx as tprev
     on tprev.BaseCur = t.BaseCur and tprev.ForCur = t.ForCur
        tprev.LastSalesRateChangeDate = t.prev_LastSalesRateChangeDate;
 
Upvote 0
The t is okay, since it's part of the function. If it doesn't work, there's some other problem.
 
Upvote 0

Forum statistics

Threads
1,221,704
Messages
6,161,390
Members
451,701
Latest member
ckrings

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top