473,406 Members | 2,710 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,406 software developers and data experts.

Help! - How can I make a calculation using a previous record?

I have an example table with fields and records as shown below:

RECORD_ID DATE PRODUCT QUANTITY
1 4/1/05 widget1 50
2 4/2/05 widget1 48
3 4/3/05 widget1 42
4 4/4/05 widget1 28
5 4/5/05 widget1 13
6 4/6/05 widget1 5
.. . . .
.. . . . (and so on)
30 4/30/05 . .

What I need is an output like this:

DATE PRODUCT SOLD (The quantity from the day before - today's)
4/2/05 widget1 2 (50 - 48)
4/3/05 widget1 6 (48 - 42)
4/4/05 widget1 14 (42 - 28)
4/5/05 widget1 15 (28 - 28)
4/6/05 . .
4/7/05 . .
4/8/05 . .
. . .
4/30/05 . . (and so on)


Calculating the quantity SOLD seems like it would be a common database
task and yet this Newbie can only come up with making 30 queries for
each day of the month. I'm wondering if a VBA module could be made to
loop through these records and perform the calculations...but I'm not a
programmer. Any help would be greatly appreciated!

Nov 13 '05 #1
6 2899
You can use the example I gave in a response to your previous post "Newbie
out of Gas - Needs Help". 05/04/2005 23:38

"Gary" <ke*****@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
I have an example table with fields and records as shown below:

RECORD_ID DATE PRODUCT QUANTITY
1 4/1/05 widget1 50
2 4/2/05 widget1 48
3 4/3/05 widget1 42
4 4/4/05 widget1 28
5 4/5/05 widget1 13
6 4/6/05 widget1 5
.. . . .
.. . . . (and so on)
30 4/30/05 . .

What I need is an output like this:

DATE PRODUCT SOLD (The quantity from the day before - today's)
4/2/05 widget1 2 (50 - 48)
4/3/05 widget1 6 (48 - 42)
4/4/05 widget1 14 (42 - 28)
4/5/05 widget1 15 (28 - 28)
4/6/05 . .
4/7/05 . .
4/8/05 . .
. . .
4/30/05 . . (and so on)


Calculating the quantity SOLD seems like it would be a common database
task and yet this Newbie can only come up with making 30 queries for
each day of the month. I'm wondering if a VBA module could be made to
loop through these records and perform the calculations...but I'm not a
programmer. Any help would be greatly appreciated!
Nov 13 '05 #2
Gary wrote:
programmer. Any help would be greatly appreciated!


tblSales
Record_ID AutoNumber, PK
SalesDate Date
ProductName Text
QuantityOnHand Long

1 4/1/05 widget1 50
2 4/2/05 widget1 48
3 4/3/05 widget1 42
4 4/1/05 widget2 25
5 4/3/05 widget2 20

qrySalesByDateAndProduct:
SELECT tblSales.SalesDate, tblSales.ProductName,
(tblSales.QuantityOnHand - (SELECT First(A.QuantityOnHand) FROM
tblSales AS A WHERE A.Record_ID > tblSales.Record_ID AND A.ProductName
= tblSales.ProductName)) AS QuantitySold FROM tblSales;

results:
SalesDate ProductName QuantitySold
4/1/05 widget1 2
4/2/05 widget1 6
4/3/05 widget1 Blank
4/1/05 widget2 5
4/2/05 widget2 Blank

I enjoy these puzzles but my deja vu sense indicates that there's a
better way to organize this information :-). Even though you can
calculate QuantitySold from the QuantityOnHand information, two
separate tables, one for the product (one record per product with a
QuantityOnHand field) and one for daily sales (one record per product
sold per day) is a more common way to do this. BTW, I sense myself
getting better at solving Access puzzles like this thanks to this NG.
I have to resort to VBA code for only the seriously tough problems. Be
sure to test this query more thoroughly if you plan on using it.

James A. Fortune

Nov 13 '05 #3
Sorry Mark

The example you gave earlier will work perfectly with the above
example. Please disregard it. I'm not very good at explaining what I
need and I apologize to you and everyone else.

In the gasoline problem I actually have 95 tanks loaded with one of
five fuel types: Regular, Super, Diesel, Mid-Grade and Kerosene. All 95
tank volumes are recorded every day, so I need to take the volume of
each tank as recorded yesterday and subtract it from each tank volume
as recorded today. The example you gave is subtracting the same min
value from all 95 tanks, and the min volume of tank_1 may not be the
min volume of tank_2. Do you see what I mean? Each tank is distinct.

I'm sorry I wish I could be clearer - the example I gave above was over
simplified and dumb of me to post.

-Gary

Nov 13 '05 #4
Thank you Jim for your efforts!

The real problem I'm having is explain more under my previous post
"Newbie out of Gas - Needs Help". 05/04/2005 23:38... but I'm doing a
poor job of explaining my problem.

My apologies to you and Mark.

-Gary

Nov 13 '05 #5
Gary,
The general case of consumption equals the difference between yesterday's
volume and today's volume is simple if you arrange your data well. Let's
assume a list of line items on invoices where products are sold. Some of
the products will sell on some line items of invoices each day. You can't
know which products will sell on a given day and therefore appear as a line
item on an invoice. What you can know is that yesterday's sales are
everything that sold the day before. So, you can query the line item table
for all lines with a sale date of date-1, or DateAdd("d",Date(),-1).
Today's sales are the same query but where sale date = today. If you join
on product then you are assured that you are comparing sales for the last
two days for the same product. Once this view is built, then it's a simple
matter of setting up an expression in the containing view that calculates
consumption = sales_yesterday - sales_today. One other wrinkle, if you want
a comprehensive list of products regardless of whether they sell on a given
day you would need an outer join between product and line item.
You've got to get off the idea that you need 31 or 30 or whatever queries
for each day of the month. Even in the gas tank example the worst case
scenario is a query for each tank/numeric measure. There were about 5
numeric measures, or five columns and 95 tanks. So, the result would have
155 columns if you needed a column per numeric measure & day of the month.
It would have 95 rows, one each for each tank. Jet crosstab queries top out
at 255 columns so this could easily be done as a Jet crosstab without any VB
coding.
--
Alan Webb
kn*******@SPAMhotmail.com
"It's not IT, it's IS
"Gary" <ke*****@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
I have an example table with fields and records as shown below:

RECORD_ID DATE PRODUCT QUANTITY
1 4/1/05 widget1 50
2 4/2/05 widget1 48
3 4/3/05 widget1 42
4 4/4/05 widget1 28
5 4/5/05 widget1 13
6 4/6/05 widget1 5
. . . .
. . . . (and so on)
30 4/30/05 . .

What I need is an output like this:

DATE PRODUCT SOLD (The quantity from the day before - today's)
4/2/05 widget1 2 (50 - 48)
4/3/05 widget1 6 (48 - 42)
4/4/05 widget1 14 (42 - 28)
4/5/05 widget1 15 (28 - 28)
4/6/05 . .
4/7/05 . .
4/8/05 . .
. . .
4/30/05 . . (and so on)


Calculating the quantity SOLD seems like it would be a common database
task and yet this Newbie can only come up with making 30 queries for
each day of the month. I'm wondering if a VBA module could be made to
loop through these records and perform the calculations...but I'm not a
programmer. Any help would be greatly appreciated!

Nov 13 '05 #6
Gary,
I said in my previous posts about the gas tank problem that I would stop
further work on the example I made until I had sample data & money. Well .
.. . I like the problem enough that I kept going on the copy of the example
database I have at home. There are additional comments in the code I
started writing and more forms. Money isn't the huge issue if I am going to
keep working as much as sample data so I can test my thinking. I can
generate fictitious gas stations but it would be better if I had a
fictitious list you generated. Write to me at the e-mail address in my
signature (minus the --NO--SPAM-- additions) if you are interested.
--
Alan Webb
kn*******@SPAMhotmail.com
"It's not IT, it's IS"

"Gary" <ke*****@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
I have an example table with fields and records as shown below:

RECORD_ID DATE PRODUCT QUANTITY
1 4/1/05 widget1 50
2 4/2/05 widget1 48
3 4/3/05 widget1 42
4 4/4/05 widget1 28
5 4/5/05 widget1 13
6 4/6/05 widget1 5
. . . .
. . . . (and so on)
30 4/30/05 . .

What I need is an output like this:

DATE PRODUCT SOLD (The quantity from the day before - today's)
4/2/05 widget1 2 (50 - 48)
4/3/05 widget1 6 (48 - 42)
4/4/05 widget1 14 (42 - 28)
4/5/05 widget1 15 (28 - 28)
4/6/05 . .
4/7/05 . .
4/8/05 . .
. . .
4/30/05 . . (and so on)


Calculating the quantity SOLD seems like it would be a common database
task and yet this Newbie can only come up with making 30 queries for
each day of the month. I'm wondering if a VBA module could be made to
loop through these records and perform the calculations...but I'm not a
programmer. Any help would be greatly appreciated!

Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help...
9
by: Tom | last post by:
A question for gui application programmers. . . I 've got some GUI programs, written in Python/wxPython, and I've got a help button and a help menu item. Also, I've got a compiled file made with...
4
by: Sarir Khamsi | last post by:
Is there a way to get help the way you get it from the Python interpreter (eg, 'help(dir)' gives help on the 'dir' command) in the module cmd.Cmd? I know how to add commands and help text to...
5
by: Craig Keightley | last post by:
Please help, i have attached my page which worksin IE but i cannnot get the drop down menu to fucntion in firefox. Any one have any ideas why? Many Thanks Craig ...
2
by: BT Openworld | last post by:
I have just had to upgrade to Access 2003 as Access 97 EMail (SendObject) doesn't work when loaded on Windows XP. I'm finding my way around Access 2003 but my biggest problem is getting...
27
by: Bruce Dodds | last post by:
I recently started using Access 2003 for the first time. I wanted to pass on some comments about the Help system to Access MVPs who frequent this board. I'm doing this in the hope that some of...
5
by: dixie | last post by:
I was wondering if there is a way of doing a simple help system with either viewing a page in a browser or looking at a definite page in a .pdf file when a help button was pushed on an Access...
5
by: TD | last post by:
Hey All, I am hooking up our custom html (.chm) help file to our Access xp application, and, despite reading several posts and manuals on this, I still have a gap in my understanding... OK, so...
1
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve...
0
by: hitencontractor | last post by:
I am working on .NET Version 2003 making an SDI application that calls MS Excel 2003. I added a menu item called "MyApp Help" in the end of the menu bar to show Help-> About. The application...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.