473,478 Members | 1,980 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

SQL Query to Calc Gas Mileage

Hi All,

I have some experience with SQL, but this one stumps me. I have
created a table to track my gas purchases (see below). But, each time
I enter a new purchase, I'm manually calculating gas mileage as
(gallons_purchased / mileage_at_purchase[n] -
mileage_at_purchase[n-1]). I can't figure out I can easilly get
gallons_purchased and mileage_at_purchase into a SQL statement, but how
do i reference the last entry's mileage_at_purchase in my SQL statement
so I can make the calculation?

prikey mileage_at_purchase gallons_purchased gas_mileage*
---------------------------------------------------------------
1 176 3.447 51.06
2 329 3.167 48.31
3 506 3.027 58.47
4 650 3.126 46.07

*I want to calculate this column.

Nov 13 '05 #1
4 2375
ja*********@gmail.com wrote:
Hi All,

I have some experience with SQL, but this one stumps me. I have
created a table to track my gas purchases (see below). But, each time
I enter a new purchase, I'm manually calculating gas mileage as
(gallons_purchased / mileage_at_purchase[n] -
mileage_at_purchase[n-1]). I can't figure out I can easilly get
gallons_purchased and mileage_at_purchase into a SQL statement, but how
do i reference the last entry's mileage_at_purchase in my SQL statement
so I can make the calculation?

prikey mileage_at_purchase gallons_purchased gas_mileage*
---------------------------------------------------------------
1 176 3.447 51.06
2 329 3.167 48.31
3 506 3.027 58.47
4 650 3.126 46.07

*I want to calculate this column.


SELECT prikey, mileage_at_purchase, gallons_purchased, (Nz(SELECT
A.mileage_at_purchase FROM tblMileage AS A WHERE A.prikey =
tblMileage.prikey - 1)) AS [PrevMileage], (mileage_at_purchase -
Nz([PrevMileage])) / gallons_purchased AS gas_mileage FROM tblMileage;

Result:

prikey mileage_at_purchase gallons_purchased PrevMileage gas_mileage
1 176 3.447 51.05889179
2 329 3.167 176 48.3107041364
3 506 3.027 329 58.4737363726
4 650 3.126 506 46.0652591171

Apply your favorate rounding method.

James A. Fortune

Nov 13 '05 #2
Ok, I've answered the first half of my own question. From a MSDN
article, I can now use the following to reference the last primary
key's values:

DLookUp("mileage_at_purchase", "tblGas", "[prikey]=" & [prikey]-1)

I now have a delima. What if my primary keys aren't sequential.
Specifically, what if I am tracking two vehicles by adding a 'vehicle'
field? Or what if I enter reciepts out of date order? I want to look
back to the last date I filled up on for the specific vehicle. The
table now looks like this:

prikey vehicle date mileage_at_purchase
gallons_purchased gas_mileage*
-----------------------------------------------------------------------------------------------------------------------
1 1 01-Jan-05 176
3.447 51.06
2 1 15-Jan-05 329
3.167 48.31
3 1 31-Jan-05 506
3.027 58.47
4 1 07-Feb-05 650
3.126 46.07
5 2 08-Feb-05 250
14.221 24.02
6 1 23-Feb-05 811
3.609 44.61

Nov 13 '05 #3
Now that's a neat trick. That's aliasing, right? What does the Nz
stand for?

Nov 13 '05 #4
ja*********@gmail.com wrote:
Ok, I've answered the first half of my own question. From a MSDN
article, I can now use the following to reference the last primary
key's values:

DLookUp("mileage_at_purchase", "tblGas", "[prikey]=" & [prikey]-1)

I now have a delima. What if my primary keys aren't sequential.
Specifically, what if I am tracking two vehicles by adding a 'vehicle'
field? Or what if I enter reciepts out of date order? I want to look
back to the last date I filled up on for the specific vehicle. The
table now looks like this:

prikey vehicle date mileage_at_purchase
gallons_purchased gas_mileage*
-----------------------------------------------------------------------------------------------------------------------
1 1 01-Jan-05 176
3.447 51.06
2 1 15-Jan-05 329
3.167 48.31
3 1 31-Jan-05 506
3.027 58.47
4 1 07-Feb-05 650
3.126 46.07
5 2 08-Feb-05 250
14.221 24.02
6 1 23-Feb-05 811
3.609 44.61


SELECT tblMileage.prikey, tblMileage.vehicleID, tblMileage.ReceiptDate,
tblMileage.mileage_at_purchase, tblMileage.gallons_purchased,
(Nz(SELECT A.mileage_at_purchase FROM tblMileage AS A WHERE A.vehicleID
= tblMileage.vehicleID AND A.ReceiptDate IN (SELECT Max(B.ReceiptDate)
FROM tblMileage As B WHERE B.ReceiptDate < tblMileage.ReceiptDate AND
B.vehicleID = tblMileage.vehicleID))) AS [PrevMileage],
(mileage_at_purchase - Nz([PrevMileage])) / gallons_purchased AS
gas_mileage
FROM tblMileage ORDER BY vehicleID, ReceiptDate;

Note: I used ReceiptDate instead of Date and vehicleID instead of
vehicle.

James A. Fortune

Nov 13 '05 #5

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

Similar topics

2
35858
by: mhodkin | last post by:
I created a query in which I have grouped data by City. I wish to calculate the percent of each value, e.g. City/(Total count of all Cities), in tbe next column of the query. I can't seem to...
2
2071
by: noname | last post by:
I have a query that subtracts this months mileage from last months, then divides that mileage value by the fuel used by the vehicle to figure this months miles per gallon. Whenever one of the...
13
4199
by: Lee | last post by:
Hello All, First of all I would like to say thank you for all of the help I have received here. I have been teaching myself Access for about 4 years now and I've always been able to find a...
6
5272
by: jimfortune | last post by:
In: http://groups.google.com/group/comp.databases.ms-access/msg/60d7faa790c65db1 james.ea...@gmail.com said: :Ok, I've answered the first half of my own question. From a MSDN :article, I...
0
1107
by: Mike Abbott | last post by:
Thanks for the replies to my earlier question I am now ready to take another step. For Tax purposes I need to show how much of the mileage of my car is business and how much is private. I...
1
3154
by: weathermanfsu | last post by:
I have a query I am running that is taking form values and basically making sure that they are not null before including them in calculations to determine the amount of $$ a person should be...
4
2514
by: dancole42 | last post by:
So I have an invoicing database based on two main forms: Orders and OrderLines. Orders has fields like: OrderID BillingMethod OrderDate CreditCard CCExp OrdSubTotal ShippingCharge
9
1789
Presto731
by: Presto731 | last post by:
I have a table that has mileage between two points. Each point has a unique id# in my query I want to be able to type in the id for point 1 and then type the id for point 2 and get the mileage. I...
15
1986
by: Widge | last post by:
Hi, I wondered if you could help me with an issue I'm having. Currently I have a rebate calculation that is running off two tables: 1) A list of suppliers and the rebate %ages relevant to them...
0
6899
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
7067
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
6847
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...
0
5312
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4463
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...
0
2980
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...
0
2970
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1288
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 ...
1
555
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.