473,545 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
+ 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_purcha sed / mileage_at_purc hase[n] -
mileage_at_purc hase[n-1]). I can't figure out I can easilly get
gallons_purchas ed and mileage_at_purc hase into a SQL statement, but how
do i reference the last entry's mileage_at_purc hase in my SQL statement
so I can make the calculation?

prikey mileage_at_purc hase gallons_purchas ed 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 2379
ja*********@gma il.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_purcha sed / mileage_at_purc hase[n] -
mileage_at_purc hase[n-1]). I can't figure out I can easilly get
gallons_purchas ed and mileage_at_purc hase into a SQL statement, but how
do i reference the last entry's mileage_at_purc hase in my SQL statement
so I can make the calculation?

prikey mileage_at_purc hase gallons_purchas ed 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_purc hase, gallons_purchas ed, (Nz(SELECT
A.mileage_at_pu rchase FROM tblMileage AS A WHERE A.prikey =
tblMileage.prik ey - 1)) AS [PrevMileage], (mileage_at_pur chase -
Nz([PrevMileage])) / gallons_purchas ed AS gas_mileage FROM tblMileage;

Result:

prikey mileage_at_purc hase gallons_purchas ed 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("mileag e_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_purc hase
gallons_purchas ed 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*********@gma il.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("mileag e_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_purc hase
gallons_purchas ed 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.prik ey, tblMileage.vehi cleID, tblMileage.Rece iptDate,
tblMileage.mile age_at_purchase , tblMileage.gall ons_purchased,
(Nz(SELECT A.mileage_at_pu rchase FROM tblMileage AS A WHERE A.vehicleID
= tblMileage.vehi cleID AND A.ReceiptDate IN (SELECT Max(B.ReceiptDa te)
FROM tblMileage As B WHERE B.ReceiptDate < tblMileage.Rece iptDate AND
B.vehicleID = tblMileage.vehi cleID))) AS [PrevMileage],
(mileage_at_pur chase - Nz([PrevMileage])) / gallons_purchas ed 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
35870
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 write the correct expression due to the "groupby" needed to group the city count in the first column. Any clues?
2
2078
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 values is 0, it returns "error#". Do I use Nz function to fix this? Here is the query SELECT .AssetID, -! AS JanMilesDriven, /! AS JanMpg, !-! AS...
13
4209
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 solution here - until now. This one is driving me crazy. I am making my first attempt at creating a runtime application. I am using Access 2003...
6
5280
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 can now use the following to reference the last primary :key's values: :
0
1108
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 have a table in which I record my business trips. It has the following fields Date /description / startmiles / endmiles
1
3160
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 reimbursed before travelling. I recently mad a couple of changes (removed a field) and now it works fine when I only have 2 days worth of reimbursement data,...
4
2522
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
1791
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 know by simply putting in the criteria field for point one i can look up that one, how do make it so you enter both id#'s and get the mileage for...
15
1993
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 2) A ongoing list of invoice details, including a month/year period and an invoice total At the moment, I can happily run the rebate calculation...
0
7479
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...
0
7411
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...
0
7926
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7439
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...
0
7773
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...
0
4962
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...
0
3468
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...
0
3450
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1901
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

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.