Query Builder - group by, min, count

G

Guest

Helloooooo :)

IN BRIEF: I built a query which returns the correct set of records. Then i
added a MIN and COUNT function but had to turn on the query builders "TOTAL"
row to get the functions to work. Which im not sure was the correct way of
doing things cause now every row seems to be "grouped by"...? Anyways,the MIN
and COUNT functions seem to return the proper results, its just one last
field ([notes]date) that im unable to retrieve.




IN DETAIL:


I have the following [tables] linked on these fields;

[Volunteers].pkVolunteerID
[Requests].fkVolunteerID
[Notes].fkVolunteerID

Each record in [Volunteers]table can have 0 to multple records in the
[notes]table but every record in the [notes]table must be attached to a
record in the [volunteers]table. (also: The [Requests] table can not have any
[notes]records attached to it. And just ONEorNONE [Volunteer]records attached
to it). The primary keys for these tables are;

[Volunteers].pkVolunteerID
[Requests].pkRequestsID
[Notes].pkNotesID


So i created a query called, "Volunteers_NotAssigned", which lists all the
[Volunteers]records that have a [Requests]record whose [Requests]EndDate
field is Null;


SELECT Volunteers.pkVolunteerID, Volunteers.FirstName, Volunteers.LastName
FROM Volunteers
WHERE (((Volunteers.[Do Not Place])=False) AND ((Volunteers.Retired)=False)
AND ((Exists (SELECT DISTINCT Requests.fkVolunteerID FROM Requests WHERE
(((Requests.fkVolunteerID) Is Not Null) AND ((Requests.EndDate) Is Null) AND
(Requests.fkVolunteerID = Volunteers.pkVolunteerID) ) ))=False));

...and it returns the proper records :) Then i wanted more information about
the returned [Volunteers]records. In particular, for every [Volunteers]record
returned in the query above i wanted to know how many [Notes]records they
have (even if its zero). And if they have a [notes]record, i also wanted to
know the number of days elapsed since the date of their most recent
[notes]record. So, i turned on the "totals" row in the query builder, and the
resulting query looks like this;

SELECT Volunteers.pkVolunteerID, Volunteers.FirstName, Volunteers.LastName,
Min(DateDiff("d",[Notes]![Date],Now())) AS DaysSinceLastNote,
Count(Notes.pkNoteID) AS NumberOfNotes
FROM Volunteers LEFT JOIN Notes ON Volunteers.pkVolunteerID =
Notes.fkVolunteerID
GROUP BY Volunteers.pkVolunteerID, Volunteers.FirstName, Volunteers.LastName
HAVING (((Volunteers.[Do Not Place])=False) AND ((Volunteers.Retired)=False)
AND ((Exists (SELECT DISTINCT Requests.fkVolunteerID FROM Requests WHERE
(((Requests.fkVolunteerID) Is Not Null) AND ((Requests.EndDate) Is Null) AND
(Requests.fkVolunteerID = Volunteers.pkVolunteerID) ) ))=False));


And that seems to work. And Now id like to get the actual value from the
[Notes]DATE field of the actual [Notes]record from the expression
"Min(DateDiff("d",[Notes]![Date],Now())) AS DaysSinceLastNote" - which would
be the **date of the most recent note***. But ive been unable to figure that
one out.


Am i going about this the right way?

Cheers,
WebDude!
 
M

[MVP] S.Clark

To be honest, I only scanned your post... briefly.
But, I've got the feeling that the info contained in the following website
is going to help you.
Please read it, and feel free to post followup questions specific to your
needs.

http://www.mvps.org/access/queries/qry0020.htm

--
Steve Clark, Access MVP
FMS, Inc.
Call us for all of your Access Development Needs!
1-888-220-6234
(e-mail address removed)
www.fmsinc.com/consulting

WebDude said:
Helloooooo :)

IN BRIEF: I built a query which returns the correct set of records. Then
i
added a MIN and COUNT function but had to turn on the query builders
"TOTAL"
row to get the functions to work. Which im not sure was the correct way of
doing things cause now every row seems to be "grouped by"...? Anyways,the
MIN
and COUNT functions seem to return the proper results, its just one last
field ([notes]date) that im unable to retrieve.

IN DETAIL:

I have the following [tables] linked on these fields;

[Volunteers].pkVolunteerID
[Requests].fkVolunteerID
[Notes].fkVolunteerID

Each record in [Volunteers]table can have 0 to multple records in the
[notes]table but every record in the [notes]table must be attached to a
record in the [volunteers]table. (also: The [Requests] table can not have
any
[notes]records attached to it. And just ONEorNONE [Volunteer]records
attached
to it). The primary keys for these tables are;

[Volunteers].pkVolunteerID
[Requests].pkRequestsID
[Notes].pkNotesID

So i created a query called, "Volunteers_NotAssigned", which lists all the
[Volunteers]records that have a [Requests]record whose [Requests]EndDate
field is Null;

SELECT Volunteers.pkVolunteerID, Volunteers.FirstName, Volunteers.LastName
FROM Volunteers
WHERE (((Volunteers.[Do Not Place])=False) AND
((Volunteers.Retired)=False)
AND ((Exists (SELECT DISTINCT Requests.fkVolunteerID FROM Requests WHERE
(((Requests.fkVolunteerID) Is Not Null) AND ((Requests.EndDate) Is Null)
AND
(Requests.fkVolunteerID = Volunteers.pkVolunteerID) ) ))=False));

..and it returns the proper records :) Then i wanted more information
about
the returned [Volunteers]records. In particular, for every
[Volunteers]record
returned in the query above i wanted to know how many [Notes]records they
have (even if its zero). And if they have a [notes]record, i also wanted
to
know the number of days elapsed since the date of their most recent
[notes]record. So, i turned on the "totals" row in the query builder, and
the
resulting query looks like this;

SELECT Volunteers.pkVolunteerID, Volunteers.FirstName,
Volunteers.LastName,
Min(DateDiff("d",[Notes]![Date],Now())) AS DaysSinceLastNote,
Count(Notes.pkNoteID) AS NumberOfNotes
FROM Volunteers LEFT JOIN Notes ON Volunteers.pkVolunteerID =
Notes.fkVolunteerID
GROUP BY Volunteers.pkVolunteerID, Volunteers.FirstName,
Volunteers.LastName
HAVING (((Volunteers.[Do Not Place])=False) AND
((Volunteers.Retired)=False)
AND ((Exists (SELECT DISTINCT Requests.fkVolunteerID FROM Requests WHERE
(((Requests.fkVolunteerID) Is Not Null) AND ((Requests.EndDate) Is Null)
AND
(Requests.fkVolunteerID = Volunteers.pkVolunteerID) ) ))=False));

And that seems to work. And Now id like to get the actual value from the
[Notes]DATE field of the actual [Notes]record from the expression
"Min(DateDiff("d",[Notes]![Date],Now())) AS DaysSinceLastNote" - which
would
be the **date of the most recent note***. But ive been unable to figure
that
one out.

Am i going about this the right way?

Cheers,
WebDude!
 
G

Guest

Thanks Clark :)

The link you provided help me turn towards the right direction.
I took out the view:totals in the querydesigngrid,
and inserted a couple of sub-queries WITH GROUPBYS in them.
:)

INGENIUS!

thanks,
jeff
 
E

expertware

Sorry, I am afraid I don't get the meaning of your words (perhaps
because I am Italian).

- Pamela
.NET developer
 

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