Need help with an average query

J

Joe Cilinceon

What I have is a table called Leases it contains a start date and end date.
What I want to do is get the average length of the lease from the start date
to the end date and if the end date is null I want to use the current date.
 
J

Joe Cilinceon

Joe said:
What I have is a table called Leases it contains a start date and end
date. What I want to do is get the average length of the lease from
the start date to the end date and if the end date is null I want to
use the current date.

I solved with Nz(EndDate,Date())
 
R

Rick Brandt

Joe said:
What I have is a table called Leases it contains a start date and end
date. What I want to do is get the average length of the lease from
the start date to the end date and if the end date is null I want to
use the current date.

SELECT LeaseID, Avg(DateDiff("d", [Start Date], Nz([End Date], Date()))) As Days
GROUP BY LeaseID
 
R

Rick Brandt

Rick said:
Joe said:
What I have is a table called Leases it contains a start date and
end date. What I want to do is get the average length of the lease
from the start date to the end date and if the end date is null I
want to use the current date.

SELECT LeaseID, Avg(DateDiff("d", [Start Date], Nz([End Date],
Date()))) As Days GROUP BY LeaseID

DOH! Obviously the LeaseID and Group By above should not be there.
 
J

Joe Cilinceon

Rick said:
Rick said:
Joe said:
What I have is a table called Leases it contains a start date and
end date. What I want to do is get the average length of the lease
from the start date to the end date and if the end date is null I
want to use the current date.

SELECT LeaseID, Avg(DateDiff("d", [Start Date], Nz([End Date],
Date()))) As Days GROUP BY LeaseID

DOH! Obviously the LeaseID and Group By above should not be there.

Actually it took me 3 queries to get what I wanted, they are listed below.

LGTHOFSTAY1
SELECT LEASES.StartDate, LEASES.EndDate,
DateDiff("m",[StartDate],Nz([EndDate],Date())) AS LengthOfStay
FROM LEASES;

LGTHOFSTAY2
SELECT Avg(LGTHOFSTAY1.LengthOfStay) AS AvgOfLengthOfStay
FROM LGTHOFSTAY1;

LGTHOFSTAY3 (Which is what I wanted)
SELECT LGTHOFSTAY2.AvgOfLengthOfStay, Int([AvgOfLengthOfStay]/12) AS Years,
Int([AvgOfLengthOfStay]-([Years]*12)) AS Months, Str([Years]) & " years " &
Str([Months]) & " months" AS AvgLengthOfStay
FROM LGTHOFSTAY2;
 

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

Top