473,698 Members | 2,574 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2912
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******** *************@g 14g2000cwa.goog legroups.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

qrySalesByDateA ndProduct:
SELECT tblSales.SalesD ate, tblSales.Produc tName,
(tblSales.Quant ityOnHand - (SELECT First(A.Quantit yOnHand) FROM
tblSales AS A WHERE A.Record_ID > tblSales.Record _ID AND A.ProductName
= tblSales.Produc tName)) 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",Dat e(),-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*******@SPAMh otmail.com
"It's not IT, it's IS
"Gary" <ke*****@gmail. com> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.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*******@SPAMh otmail.com
"It's not IT, it's IS"

"Gary" <ke*****@gmail. com> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.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
6546
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 Workshop program: hcw.exe that's included with Visual Basic. This exact same file compiled perfectly with no notes, warnings or errors prior to reformatting my system. Prior to the reformatting, I copied the help.rtf file onto a CD and checked the box to...
9
4401
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 the microsoft HTML workshop utility, lets call it c:\path\help.chm. My question is how do you launch it from the GUI? What logic do I put behind the "help" button, in other words. I thought it would be os.spawnv(os.P_DETACH,...
4
3350
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 cmd.Cmd but I would also like to get the man-page-like help for classes and functions. Does anyone know how to do that? Thanks. Sarir
5
2994
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 <<<<<<<<<<<<<<CODE>>>>>>>>>>>>>>>> <html>
2
3940
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 information. When I bought Access 1.0 many years ago, I got a paper manual and an excellent help file with all you needed to know. With Access 97 the manual came as a 'Building Applications' document on the CD and I still had the excellent help. With Access...
27
2814
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 what I say may be passed on to Microsoft. Microsoft has implemented a combination local and web based help system in Access 2003. The local Help installed on your hard drive is fast, but the content is abbreviated. For really useful help you have...
5
1762
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 application form. I understand there are other ways of doing help files, but are these possible, what is the easiest and how much work is it? dixie
5
2613
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 I've setup the registry keys to have a profile and I specify an AppHelpFile and TitleBar in that profile. This works swell: when I startup my application in runtime with the /profile option and then I press F1 on a form, our help file appears....
1
6128
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 default property of object Label. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"' Label = New Object(){Box1, Box2, Box3, Box4, Box5, Box6, Box7, Box8, Box9, Box10, Box11,...
0
2883
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 calls MS Excel, so the scenario is that I am supposed to see the Excel Menu bar, FILE EDIT VIEW INSERT ... HELP. I am able to see the menu bar, but in case of Help, I see the Help of Excel and help of my application, both as a submenu of help. ...
0
8683
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8610
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8902
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8873
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4623
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.