DatePart

M

Melinda

I have a Vehicle database with a table for odometer
readings:

tbl_Odometer_Readings
strVehicleID
datMonthEndDate (mm/dd/yyyy)
intOdometerReading

I am trying to write a query to calculate miles driven by
subtracting odometer readings. I can get every month
except January where I have to subtract December from the
prior year. I don't use VBA very much, so if I could get
some help in criteria expression in the query, I would
appreciate it.

Melinda
 
M

Michel Walsh

Hi,



SELECT a.VehiculeID,
a.MonthEndDate,
MIN(b.OdometerReading) - LAST(a.OdometerReading) AS difference

FROM OdometerReadings As a INNER JOIN OdometerReadings As b
ON a.VehiculeID = b.VehiculeID
AND b.MonthEndDate > a.MonthEndDate

GROUP BY a.VehiculeID, a.MonthEndDate



Note that in SQL, as in recent programming languages (as C# and dot-Net),
the practice is to NOT use prefix (tbl, str, dat, int), since they add
notice more than anything really useful (strongly typed languages would
report data type mismatch at compile time, anyhow), but do as you wish.


Hoping it may help,
Vanderghast, Access MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top