473,320 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

finding percent change between two rows

Hi,
This is driving me crazy, but I am sure I am missing something simple. I have built an Access 2007 report that shows 2 rows of sales data from each of a bunch of store locations. My table has the date, the location and the sales_amount. I need to have my report group on locations. The resulting rows include a date and the sales_amount, and I have been able to display the sales_amount for a requested date (through a parameter query) and the sales_amount from the same week in the previous year, and these rows are shown for each location. But I now need to be able to show the $ change in sales_amount and the percent change in sales_amount for each location between the current and previous year. This means that I somehow need to access data from (both of) the grouped rows and calculate and display the results. How do I access data from multiple rows? Do I have to create the report manually using VBA, and if so, any examples out there?

Here is an example of what I am trying to accomplish:
Table Sales has Location, Date and SalesAmount, and records such as:

loc1, 10/10/2007, $40
loc2, 10/10/2007, $40
loc3, 10/10/2007, $60
loc1, 10/10/2008, $50
loc2, 10/10/2008, $30
loc3, 10/10/2008, $70

I want a report that shows:
--- start of report ---

loc1
10/10/2008 $50
10/10/2007 $40
$change = $10 +25%
loc2
10/10/2008 $30
10/10/2007 $40
$change = -$10 -33%
loc2
10/10/2008 $70
10/10/2007 $60
$ change - $10 +17%

----- end of report -----
Oct 25 '08 #1
4 4308
NeoPa
32,556 Expert Mod 16PB
This will need to be done in code. Queries (SQL) have no concept of relative records.
Oct 25 '08 #2
ADezii
8,834 Expert 8TB
Too close to bedtime, but given your demonstrated format, this code will produce the desired results, but only in a Linear Fashion:
Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String
  2. Dim MyDB As DAO.Database
  3. Dim rstSales As DAO.Recordset
  4. Dim rstClone As DAO.Recordset
  5.  
  6. strSQL = "SELECT Sales.Location, Sales.Date, Sales.[Sales Amount] " & _
  7.          "FROM Sales ORDER BY Sales.Location, Sales.Date DESC;"
  8.  
  9. Set MyDB = CurrentDb
  10. Set rstSales = MyDB.OpenRecordset(strSQL, dbOpenDynaset)
  11. Set rstClone = rstSales.Clone
  12.  
  13. rstSales.MoveFirst
  14. rstClone.MoveFirst
  15. rstClone.Move 1     'Move to the 2nd Record in Clone
  16.  
  17. With rstSales
  18.   Do Until rstClone.EOF
  19.     If ![Location] = rstClone![Location] Then
  20.       Debug.Print ![Location] & " | " & ![Date] & " | " & ![Sales Amount] & _
  21.                   " | " & rstClone![Date] & " | " & rstClone![Sales Amount] & " | " & _
  22.                   Format$(![Sales Amount] - rstClone![Sales Amount], "Currency") & _
  23.                   " | " & Format$((![Sales Amount] - rstClone![Sales Amount]) / ![Sales Amount], "Percent")
  24.     End If
  25.     rstClone.MoveNext
  26.     .MoveNext
  27.   Loop
  28. End With
  29.  
  30. rstSales.Close
  31. rstClone.Close
  32. Set rstSales = Nothing
  33. Set rstClone = Nothing
OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. loc1 | 10/10/2008 | 50 | 10/10/2007 | 40 | $10.00 | 20.00%
  2. loc2 | 10/10/2008 | 30 | 10/10/2007 | 40 | ($10.00) | -33.33%
  3. loc3 | 10/10/2008 | 70 | 10/10/2007 | 60 | $10.00 | 14.29%
NOTE: Any questions, feel free to ask.
Oct 26 '08 #3
Thank you very much. So I have to use VBA and create a custom Report, which in hindsight, seems obvious. I will go and get a Access/VBA book and figure out the details, but this has helped me get started.
Oct 27 '08 #4
NeoPa
32,556 Expert Mod 16PB
Sounds like a plan :)

We're here if you need help of course.

Welcome to Bytes!
Oct 27 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: rozrabiak | last post by:
Hi! How can I get percent of executed procedure in MSSQL Server, lik in Enterprise Manager and/or dbMgr2k ? I use Visual C# and MSDE. Thank's for help, gregory
3
by: JohnnyRuin | last post by:
Hi, My table has int Field1 and DateTime dtModificationTime. I maintain a history of changes to the table by inserting a new record with a new dtModificationTime. I need to be able to find...
4
by: MX1 | last post by:
Hi all, I've setup a table with one field that will hold percent values. The type is number and the format is percent on the field. When I do data entry directly into the field, I have to put...
0
by: Macbeth | last post by:
Thanks to All who Help, I am creating a crosstab querry to place in a report. All my data is correct and I do not seem to have any problem with this area. I am having problems with displaying...
10
by: tshad | last post by:
I have a Datagrid with a column: <asp:HyperLinkColumn DataTextField="JobTitle" DataNavigateUrlField="PositionID" DataNavigateUrlFormatString="AddNewPositions.aspx?PositionID={0}"...
39
by: VidTheKid | last post by:
THE PROBLEM The % symbol is too vague when defining dimensions in CSS and HTML. It can relate to an inherited value, a measure of the containing element (which can differ between box models) or...
14
by: prasadjoshi124 | last post by:
Hi All, I am writing a small tool which is supposed to fill the filesystem to a specified percent. For, that I need to read how much the file system is full in percent, like the output given...
1
by: nycjay | last post by:
I am trying to do the following w/ SQL for db2 9... I want to find the top x percent of rows. For example, if i wanted to find the average sale price of the top 10 most expensive items in a store,...
7
by: Bernice J | last post by:
I have a table with four fields: BlockNo, District, SubDate, Status. I would like to create a report that 1)shows the percent of BlockNo that has a SubDate(IsNotNull) grouped by District and 2) shows...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.