Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    natonstan is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Oct 2014
    Posts
    37

    Percentage Difference Between two fields?

    I have a table named tlbForEx, there's 2 columns in that table one called ForExRate and one called SalesRate, I need to make a query that takes those 2 fields and works out the percentage difference between the two, is there any easy way to do this? My SQL experience is very limited currently..

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,549
    In a query, Make a 3rd field,( ForExRate-SalesRate)

  3. #3
    natonstan is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Oct 2014
    Posts
    37
    Hi ranman, thanks for the help, so I have my blank query:

    SELECT tblForEx.ForExRate, SalesRate;
    FROM tblForEx;

    How would I add the 3rd field creation to this?

  4. #4
    natonstan is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Oct 2014
    Posts
    37
    Okay, scrap that question, I got it working by simply adding:

    ,(ForExRate-SalesRate)

    Now how would I get that to show as a percentage, I've tried using AS Percent, but I'm getting an error saying incorrect spelling of Percent?

  5. #5
    Micron is offline Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,372
    , Format(ForExRate-SalesRate, percent)?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 7 32bit Access 2013
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,610
    Percentage of what. ForExRate or SalesRate
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  7. #7
    natonstan is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Oct 2014
    Posts
    37
    I need it to display the average as a percentage, so where I have (ForExRate-SalesRate) I need that total to be in a percentage

    I've tried adding , Format(ForExRate-SalesRate, percent)

    But I get an input box asking for a percentage

  8. #8
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2007
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    Do you mean the mathematical idea of a % increase or decrease

    that would be

    Code:
    Format((ForExRate-SalesRate)/salesrate, "percent")

  9. #9
    natonstan is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Oct 2014
    Posts
    37
    I think I may be going about this the wrong way now that I look at it, the main goal of this is to see if there is a -/+ 5% difference in the ForEx Rate since the LastSalesRateChangeDate column changed.

    So I think I could probably simplify it a bit, basically I would need for my query to check the LastSalesRateChangeDate in the tblForEx table, then look at the ForExRate field and calculate the percentage since the last time the LastSalesRateChangeDate changed.

    I hope that is clear enough, it sounds clear in my head but actually writing out a query is confusing me slightly..

  10. #10
    natonstan is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Oct 2014
    Posts
    37
    I received this help from someone, but I can't work out if the 't's are supposed to be in reference to my table tblForEx, would I essentially replace all 't' to 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;

  11. #11
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2007
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    doesnt it work as it is

    If not, what does it produce

  12. #12
    natonstan is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Oct 2014
    Posts
    37
    Andy, if I try to run that code as is, I get Syntax Error (missing operator) in query expression '', when I click ok it highlights a section in the second to last line, the tprev.ForCur (but weirdly only the 'ev.Fo' letters

  13. #13
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2007
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    I think your missing an AND at the end of the second last line

  14. #14
    natonstan is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Oct 2014
    Posts
    37
    Do you mean I need an ; at the end of the second line?

    If I do that I get "Character found after end of SQL statement

  15. #15
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2007
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    Quote Originally Posted by andy49 View Post
    I think your missing an AND at the end of the second last line
    typed it too late

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 06-27-2016, 03:09 PM
  2. Replies: 2
    Last Post: 02-11-2013, 01:26 PM
  3. Replies: 10
    Last Post: 10-19-2012, 05:10 AM
  4. Replies: 1
    Last Post: 10-09-2012, 09:08 AM
  5. Add fields and find percentage
    By gurp99 in forum Queries
    Replies: 1
    Last Post: 08-09-2010, 03:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums