Getting a related field from a GroupBy (total) query

J

Jon Lewis

Hi All

The above subject continues to baffle me!
http://www.mvps.org/access/queries/qry0020.htm
This link was posted a few messages ago but IMO these methods don't work
when there are duplicate combinations of the GroupBy and Totalled Fields in
the data source.

Consider the following:
TaskID ContactID Due Type
1 1 01/04/2007 Call
2 1 01/04/2007 Write
3 1 01/05/2007 Email
4 2 01/04/2007 Call
5 2 01/05/2007 Write
6 2 01/05/2007 Email
7 3 01/06/2007 Fax
8 4 01/05/2007 Call
9 5 01/07/2007 Email
10 5 01/07/2007 Call
11 5 01/07/2007 Call


Each task (PK) relates to a contact (FK) and has a due date and type

How do I get the earliest due date of a task for each contact and a related
field (Type or TaskID). When there are duplicate ContactID & Due
combinations then the first record should be returned (the lowest TaskID
would make sense).

SELECT tblTasks.ContactID, Min(tblTasks.Due) AS MinOfDue,
First(tblTasks.TaskID) AS FirstOfTaskID
FROM tblTasks
GROUP BY tblTasks.ContactID;

appears to work (returns 5 records) but I know from experience that
FirstOfTaskID could return a TaskID from a different record. Whilst it
works with the above data, it does not work with the table from which this
data is sampled although I can't figure out why.

The methods in the above link don't work .
e.g
SELECT Q.ContactID, Q.TaskID
FROM tblTasks AS Q INNER JOIN (SELECT ContactID, Min(Due) As S
FROM tblTasks
GROUP BY ContactID) AS T ON (Q.Due=T.S) AND (Q.ContactId=T.ContactId);

returns 8 rows of ContactID and TaskID
I'd like a query (or nest of queries) to return 5 records (TaskID's 1, 4,
7,8,9) - can anyone help please?

TIA
 
J

Jon Lewis

Thanks for your response Crystal
When applied to my real data ,the query you suggested returns 199 rows
whereas

SELECT tblTasks.ContactID, Min(tblTasks.Due) AS MinOfDue
FROM tblTasks
GROUP BY tblTasks.ContactID

returns 844 rows. I need to grab the TaskID for these 844 rows.
Also dLookup is slow even when executed only once. In this context the
query takes >7 secs to execute and that's with
the data mdb linked on the same development PC as the system MDB. Over a
network it will be much longer.

Like I said in my post, the conventional documented methods don't seem to
work when there are duplicate combinations of the Grouped and Totalled
fields.

SELECT Q.ContactID, Q.TaskID
FROM tblTasks AS Q INNER JOIN (SELECT ContactID, Min(Due) As S
FROM tblTasks
GROUP BY ContactID) AS T ON (Q.Due=T.S) AND (Q.ContactId=T.ContactId);

Returns additional rows (duplicate ContactID's) for duplicate combinations
of Q.Due=T.S AND Q.ContactId=T.ContactId

However, If I save this query as qryTest then

SELECT qryTest.ContactID, First(qryTest.TaskID) AS FirstOfTaskID
FROM qryTest
GROUP BY qryTest.ContactID;

gives the desired result (I think) so I just need to figure out how to nest
these queries

Tomorrow as my brain is now addled and it's Midnight

BTW my name is Jon

Many thanks





strive4peace said:
get related information for minimum date
---

Hi (what is your name?),

try this:
SELECT ContactID, Min(Due) AS MinOfDue, TaskID
FROM tblTasks
WHERE Due = dLookup("Due","tblTasks","ContactID=" & [ContactID]))
GROUP BY ContactID, TaskID;


Warm Regards,
Crystal
*
:) have an awesome day :)
*
MVP Access
Remote Programming and Training
strive4peace2006 at yahoo.com
*


Hi All

The above subject continues to baffle me!
http://www.mvps.org/access/queries/qry0020.htm
This link was posted a few messages ago but IMO these methods don't work
when there are duplicate combinations of the GroupBy and Totalled Fields
in the data source.

Consider the following:
TaskID ContactID Due Type
1 1 01/04/2007 Call
2 1 01/04/2007 Write
3 1 01/05/2007 Email
4 2 01/04/2007 Call
5 2 01/05/2007 Write
6 2 01/05/2007 Email
7 3 01/06/2007 Fax
8 4 01/05/2007 Call
9 5 01/07/2007 Email
10 5 01/07/2007 Call
11 5 01/07/2007 Call


Each task (PK) relates to a contact (FK) and has a due date and type

How do I get the earliest due date of a task for each contact and a
related field (Type or TaskID). When there are duplicate ContactID & Due
combinations then the first record should be returned (the lowest TaskID
would make sense).

SELECT tblTasks.ContactID, Min(tblTasks.Due) AS MinOfDue,
First(tblTasks.TaskID) AS FirstOfTaskID
FROM tblTasks
GROUP BY tblTasks.ContactID;

appears to work (returns 5 records) but I know from experience that
FirstOfTaskID could return a TaskID from a different record. Whilst it
works with the above data, it does not work with the table from which
this data is sampled although I can't figure out why.

The methods in the above link don't work .
e.g
SELECT Q.ContactID, Q.TaskID
FROM tblTasks AS Q INNER JOIN (SELECT ContactID, Min(Due) As S
FROM tblTasks
GROUP BY ContactID) AS T ON (Q.Due=T.S) AND (Q.ContactId=T.ContactId);

returns 8 rows of ContactID and TaskID
I'd like a query (or nest of queries) to return 5 records (TaskID's 1, 4,
7,8,9) - can anyone help please?

TIA
 
S

strive4peace

Hi Jon,

Many apologies! I did not mean dLookup, I meant to put dMin!

The logic is this: set the criteria for the date to the date with the
minimum value -- then you can return all the related data on the record too.

SELECT ContactID, Due, TaskID
FROM tblTasks
WHERE Due = dMin("Due","tblTasks"
,"ContactID=" & [ContactID]
& " AND TaskID=" & [TaskID])
GROUP BY ContactID, TaskID;

If TaskID or ContactID are not numbers (which they should be) then you
will need delimiters

Warm Regards,
Crystal
*
:) have an awesome day :)
*
MVP Access
Remote Programming and Training
strive4peace2006 at yahoo.com
*



Jon said:
Thanks for your response Crystal
When applied to my real data ,the query you suggested returns 199 rows
whereas

SELECT tblTasks.ContactID, Min(tblTasks.Due) AS MinOfDue
FROM tblTasks
GROUP BY tblTasks.ContactID

returns 844 rows. I need to grab the TaskID for these 844 rows.
Also dLookup is slow even when executed only once. In this context the
query takes >7 secs to execute and that's with
the data mdb linked on the same development PC as the system MDB. Over a
network it will be much longer.

Like I said in my post, the conventional documented methods don't seem to
work when there are duplicate combinations of the Grouped and Totalled
fields.

SELECT Q.ContactID, Q.TaskID
FROM tblTasks AS Q INNER JOIN (SELECT ContactID, Min(Due) As S
FROM tblTasks
GROUP BY ContactID) AS T ON (Q.Due=T.S) AND (Q.ContactId=T.ContactId);

Returns additional rows (duplicate ContactID's) for duplicate combinations
of Q.Due=T.S AND Q.ContactId=T.ContactId

However, If I save this query as qryTest then

SELECT qryTest.ContactID, First(qryTest.TaskID) AS FirstOfTaskID
FROM qryTest
GROUP BY qryTest.ContactID;

gives the desired result (I think) so I just need to figure out how to nest
these queries

Tomorrow as my brain is now addled and it's Midnight

BTW my name is Jon

Many thanks





strive4peace said:
get related information for minimum date
---

Hi (what is your name?),

try this:
SELECT ContactID, Min(Due) AS MinOfDue, TaskID
FROM tblTasks
WHERE Due = dLookup("Due","tblTasks","ContactID=" & [ContactID]))
GROUP BY ContactID, TaskID;


Warm Regards,
Crystal
*
:) have an awesome day :)
*
MVP Access
Remote Programming and Training
strive4peace2006 at yahoo.com
*


Hi All

The above subject continues to baffle me!
http://www.mvps.org/access/queries/qry0020.htm
This link was posted a few messages ago but IMO these methods don't work
when there are duplicate combinations of the GroupBy and Totalled Fields
in the data source.

Consider the following:
TaskID ContactID Due Type
1 1 01/04/2007 Call
2 1 01/04/2007 Write
3 1 01/05/2007 Email
4 2 01/04/2007 Call
5 2 01/05/2007 Write
6 2 01/05/2007 Email
7 3 01/06/2007 Fax
8 4 01/05/2007 Call
9 5 01/07/2007 Email
10 5 01/07/2007 Call
11 5 01/07/2007 Call


Each task (PK) relates to a contact (FK) and has a due date and type

How do I get the earliest due date of a task for each contact and a
related field (Type or TaskID). When there are duplicate ContactID & Due
combinations then the first record should be returned (the lowest TaskID
would make sense).

SELECT tblTasks.ContactID, Min(tblTasks.Due) AS MinOfDue,
First(tblTasks.TaskID) AS FirstOfTaskID
FROM tblTasks
GROUP BY tblTasks.ContactID;

appears to work (returns 5 records) but I know from experience that
FirstOfTaskID could return a TaskID from a different record. Whilst it
works with the above data, it does not work with the table from which
this data is sampled although I can't figure out why.

The methods in the above link don't work .
e.g
SELECT Q.ContactID, Q.TaskID
FROM tblTasks AS Q INNER JOIN (SELECT ContactID, Min(Due) As S
FROM tblTasks
GROUP BY ContactID) AS T ON (Q.Due=T.S) AND (Q.ContactId=T.ContactId);

returns 8 rows of ContactID and TaskID
I'd like a query (or nest of queries) to return 5 records (TaskID's 1, 4,
7,8,9) - can anyone help please?

TIA
 
J

Jon Lewis

Hi Crystal

This won't execute because SELECT ContactID, Due, TaskID requires that Due
be included in the aggregate function ( GROUP BY ContactID, TaskID)

But the following is now working correctly:

SELECT Q.ContactID, Min(Q.TaskID) AS MinOfTaskID
FROM tblTasks AS Q INNER JOIN tblTasks AS T ON Q.ContactID = T.ContactID
GROUP BY Q.ContactID, Q.Due
HAVING (((Q.Due)=Max([T].[Due])));

Thanks for your help

Jon


strive4peace said:
Hi Jon,

Many apologies! I did not mean dLookup, I meant to put dMin!

The logic is this: set the criteria for the date to the date with the
minimum value -- then you can return all the related data on the record
too.

SELECT ContactID, Due, TaskID
FROM tblTasks
WHERE Due = dMin("Due","tblTasks"
,"ContactID=" & [ContactID]
& " AND TaskID=" & [TaskID])
GROUP BY ContactID, TaskID;

If TaskID or ContactID are not numbers (which they should be) then you
will need delimiters

Warm Regards,
Crystal
*
:) have an awesome day :)
*
MVP Access
Remote Programming and Training
strive4peace2006 at yahoo.com
*



Jon said:
Thanks for your response Crystal
When applied to my real data ,the query you suggested returns 199 rows
whereas

SELECT tblTasks.ContactID, Min(tblTasks.Due) AS MinOfDue
FROM tblTasks
GROUP BY tblTasks.ContactID

returns 844 rows. I need to grab the TaskID for these 844 rows.
Also dLookup is slow even when executed only once. In this context the
query takes >7 secs to execute and that's with
the data mdb linked on the same development PC as the system MDB. Over a
network it will be much longer.

Like I said in my post, the conventional documented methods don't seem to
work when there are duplicate combinations of the Grouped and Totalled
fields.

SELECT Q.ContactID, Q.TaskID
FROM tblTasks AS Q INNER JOIN (SELECT ContactID, Min(Due) As S
FROM tblTasks
GROUP BY ContactID) AS T ON (Q.Due=T.S) AND (Q.ContactId=T.ContactId);

Returns additional rows (duplicate ContactID's) for duplicate
combinations of Q.Due=T.S AND Q.ContactId=T.ContactId

However, If I save this query as qryTest then

SELECT qryTest.ContactID, First(qryTest.TaskID) AS FirstOfTaskID
FROM qryTest
GROUP BY qryTest.ContactID;

gives the desired result (I think) so I just need to figure out how to
nest these queries

Tomorrow as my brain is now addled and it's Midnight

BTW my name is Jon

Many thanks





strive4peace said:
get related information for minimum date
---

Hi (what is your name?),

try this:
SELECT ContactID, Min(Due) AS MinOfDue, TaskID
FROM tblTasks
WHERE Due = dLookup("Due","tblTasks","ContactID=" & [ContactID]))
GROUP BY ContactID, TaskID;


Warm Regards,
Crystal
*
:) have an awesome day :)
*
MVP Access
Remote Programming and Training
strive4peace2006 at yahoo.com
*



Jon Lewis <jon.lewis wrote:
Hi All

The above subject continues to baffle me!
http://www.mvps.org/access/queries/qry0020.htm
This link was posted a few messages ago but IMO these methods don't
work when there are duplicate combinations of the GroupBy and Totalled
Fields in the data source.

Consider the following:
TaskID ContactID Due Type
1 1 01/04/2007 Call
2 1 01/04/2007 Write
3 1 01/05/2007 Email
4 2 01/04/2007 Call
5 2 01/05/2007 Write
6 2 01/05/2007 Email
7 3 01/06/2007 Fax
8 4 01/05/2007 Call
9 5 01/07/2007 Email
10 5 01/07/2007 Call
11 5 01/07/2007 Call


Each task (PK) relates to a contact (FK) and has a due date and type

How do I get the earliest due date of a task for each contact and a
related field (Type or TaskID). When there are duplicate ContactID &
Due combinations then the first record should be returned (the lowest
TaskID would make sense).

SELECT tblTasks.ContactID, Min(tblTasks.Due) AS MinOfDue,
First(tblTasks.TaskID) AS FirstOfTaskID
FROM tblTasks
GROUP BY tblTasks.ContactID;

appears to work (returns 5 records) but I know from experience that
FirstOfTaskID could return a TaskID from a different record. Whilst it
works with the above data, it does not work with the table from which
this data is sampled although I can't figure out why.

The methods in the above link don't work .
e.g
SELECT Q.ContactID, Q.TaskID
FROM tblTasks AS Q INNER JOIN (SELECT ContactID, Min(Due) As S
FROM tblTasks
GROUP BY ContactID) AS T ON (Q.Due=T.S) AND (Q.ContactId=T.ContactId);

returns 8 rows of ContactID and TaskID
I'd like a query (or nest of queries) to return 5 records (TaskID's 1,
4, 7,8,9) - can anyone help please?

TIA
 
J

Jon Lewis

Should be:

SELECT Q.ContactID, Min(Q.TaskID) AS MinOfTaskID
FROM tblTasks AS Q INNER JOIN tblTasks AS T ON Q.ContactID = T.ContactID
GROUP BY Q.ContactID, Q.Due
HAVING (((Q.Due)=Min([T].[Due])));

strive4peace said:
Hi Jon,

Many apologies! I did not mean dLookup, I meant to put dMin!

The logic is this: set the criteria for the date to the date with the
minimum value -- then you can return all the related data on the record
too.

SELECT ContactID, Due, TaskID
FROM tblTasks
WHERE Due = dMin("Due","tblTasks"
,"ContactID=" & [ContactID]
& " AND TaskID=" & [TaskID])
GROUP BY ContactID, TaskID;

If TaskID or ContactID are not numbers (which they should be) then you
will need delimiters

Warm Regards,
Crystal
*
:) have an awesome day :)
*
MVP Access
Remote Programming and Training
strive4peace2006 at yahoo.com
*



Jon said:
Thanks for your response Crystal
When applied to my real data ,the query you suggested returns 199 rows
whereas

SELECT tblTasks.ContactID, Min(tblTasks.Due) AS MinOfDue
FROM tblTasks
GROUP BY tblTasks.ContactID

returns 844 rows. I need to grab the TaskID for these 844 rows.
Also dLookup is slow even when executed only once. In this context the
query takes >7 secs to execute and that's with
the data mdb linked on the same development PC as the system MDB. Over a
network it will be much longer.

Like I said in my post, the conventional documented methods don't seem to
work when there are duplicate combinations of the Grouped and Totalled
fields.

SELECT Q.ContactID, Q.TaskID
FROM tblTasks AS Q INNER JOIN (SELECT ContactID, Min(Due) As S
FROM tblTasks
GROUP BY ContactID) AS T ON (Q.Due=T.S) AND (Q.ContactId=T.ContactId);

Returns additional rows (duplicate ContactID's) for duplicate
combinations of Q.Due=T.S AND Q.ContactId=T.ContactId

However, If I save this query as qryTest then

SELECT qryTest.ContactID, First(qryTest.TaskID) AS FirstOfTaskID
FROM qryTest
GROUP BY qryTest.ContactID;

gives the desired result (I think) so I just need to figure out how to
nest these queries

Tomorrow as my brain is now addled and it's Midnight

BTW my name is Jon

Many thanks





strive4peace said:
get related information for minimum date
---

Hi (what is your name?),

try this:
SELECT ContactID, Min(Due) AS MinOfDue, TaskID
FROM tblTasks
WHERE Due = dLookup("Due","tblTasks","ContactID=" & [ContactID]))
GROUP BY ContactID, TaskID;


Warm Regards,
Crystal
*
:) have an awesome day :)
*
MVP Access
Remote Programming and Training
strive4peace2006 at yahoo.com
*



Jon Lewis <jon.lewis wrote:
Hi All

The above subject continues to baffle me!
http://www.mvps.org/access/queries/qry0020.htm
This link was posted a few messages ago but IMO these methods don't
work when there are duplicate combinations of the GroupBy and Totalled
Fields in the data source.

Consider the following:
TaskID ContactID Due Type
1 1 01/04/2007 Call
2 1 01/04/2007 Write
3 1 01/05/2007 Email
4 2 01/04/2007 Call
5 2 01/05/2007 Write
6 2 01/05/2007 Email
7 3 01/06/2007 Fax
8 4 01/05/2007 Call
9 5 01/07/2007 Email
10 5 01/07/2007 Call
11 5 01/07/2007 Call


Each task (PK) relates to a contact (FK) and has a due date and type

How do I get the earliest due date of a task for each contact and a
related field (Type or TaskID). When there are duplicate ContactID &
Due combinations then the first record should be returned (the lowest
TaskID would make sense).

SELECT tblTasks.ContactID, Min(tblTasks.Due) AS MinOfDue,
First(tblTasks.TaskID) AS FirstOfTaskID
FROM tblTasks
GROUP BY tblTasks.ContactID;

appears to work (returns 5 records) but I know from experience that
FirstOfTaskID could return a TaskID from a different record. Whilst it
works with the above data, it does not work with the table from which
this data is sampled although I can't figure out why.

The methods in the above link don't work .
e.g
SELECT Q.ContactID, Q.TaskID
FROM tblTasks AS Q INNER JOIN (SELECT ContactID, Min(Due) As S
FROM tblTasks
GROUP BY ContactID) AS T ON (Q.Due=T.S) AND (Q.ContactId=T.ContactId);

returns 8 rows of ContactID and TaskID
I'd like a query (or nest of queries) to return 5 records (TaskID's 1,
4, 7,8,9) - can anyone help please?

TIA
 
S

strive4peace

Hi Jon,

you're welcome

sorry again, I am unloading boxes today (in the snow) from a moving van
and coming inside to take breaks to thaw my feet and hands -- not in top
shape but I am glad you got it!

Warm Regards,
Crystal
*
:) have an awesome day :)
*
MVP Access
Remote Programming and Training
strive4peace2006 at yahoo.com
*


Hi Crystal

This won't execute because SELECT ContactID, Due, TaskID requires that Due
be included in the aggregate function ( GROUP BY ContactID, TaskID)

But the following is now working correctly:

SELECT Q.ContactID, Min(Q.TaskID) AS MinOfTaskID
FROM tblTasks AS Q INNER JOIN tblTasks AS T ON Q.ContactID = T.ContactID
GROUP BY Q.ContactID, Q.Due
HAVING (((Q.Due)=Max([T].[Due])));

Thanks for your help

Jon


strive4peace said:
Hi Jon,

Many apologies! I did not mean dLookup, I meant to put dMin!

The logic is this: set the criteria for the date to the date with the
minimum value -- then you can return all the related data on the record
too.

SELECT ContactID, Due, TaskID
FROM tblTasks
WHERE Due = dMin("Due","tblTasks"
,"ContactID=" & [ContactID]
& " AND TaskID=" & [TaskID])
GROUP BY ContactID, TaskID;

If TaskID or ContactID are not numbers (which they should be) then you
will need delimiters

Warm Regards,
Crystal
*
:) have an awesome day :)
*
MVP Access
Remote Programming and Training
strive4peace2006 at yahoo.com
*



Jon said:
Thanks for your response Crystal
When applied to my real data ,the query you suggested returns 199 rows
whereas

SELECT tblTasks.ContactID, Min(tblTasks.Due) AS MinOfDue
FROM tblTasks
GROUP BY tblTasks.ContactID

returns 844 rows. I need to grab the TaskID for these 844 rows.
Also dLookup is slow even when executed only once. In this context the
query takes >7 secs to execute and that's with
the data mdb linked on the same development PC as the system MDB. Over a
network it will be much longer.

Like I said in my post, the conventional documented methods don't seem to
work when there are duplicate combinations of the Grouped and Totalled
fields.

SELECT Q.ContactID, Q.TaskID
FROM tblTasks AS Q INNER JOIN (SELECT ContactID, Min(Due) As S
FROM tblTasks
GROUP BY ContactID) AS T ON (Q.Due=T.S) AND (Q.ContactId=T.ContactId);

Returns additional rows (duplicate ContactID's) for duplicate
combinations of Q.Due=T.S AND Q.ContactId=T.ContactId

However, If I save this query as qryTest then

SELECT qryTest.ContactID, First(qryTest.TaskID) AS FirstOfTaskID
FROM qryTest
GROUP BY qryTest.ContactID;

gives the desired result (I think) so I just need to figure out how to
nest these queries

Tomorrow as my brain is now addled and it's Midnight

BTW my name is Jon

Many thanks





get related information for minimum date
---

Hi (what is your name?),

try this:
SELECT ContactID, Min(Due) AS MinOfDue, TaskID
FROM tblTasks
WHERE Due = dLookup("Due","tblTasks","ContactID=" & [ContactID]))
GROUP BY ContactID, TaskID;


Warm Regards,
Crystal
*
:) have an awesome day :)
*
MVP Access
Remote Programming and Training
strive4peace2006 at yahoo.com
*



Jon Lewis <jon.lewis wrote:
Hi All

The above subject continues to baffle me!
http://www.mvps.org/access/queries/qry0020.htm
This link was posted a few messages ago but IMO these methods don't
work when there are duplicate combinations of the GroupBy and Totalled
Fields in the data source.

Consider the following:
TaskID ContactID Due Type
1 1 01/04/2007 Call
2 1 01/04/2007 Write
3 1 01/05/2007 Email
4 2 01/04/2007 Call
5 2 01/05/2007 Write
6 2 01/05/2007 Email
7 3 01/06/2007 Fax
8 4 01/05/2007 Call
9 5 01/07/2007 Email
10 5 01/07/2007 Call
11 5 01/07/2007 Call


Each task (PK) relates to a contact (FK) and has a due date and type

How do I get the earliest due date of a task for each contact and a
related field (Type or TaskID). When there are duplicate ContactID &
Due combinations then the first record should be returned (the lowest
TaskID would make sense).

SELECT tblTasks.ContactID, Min(tblTasks.Due) AS MinOfDue,
First(tblTasks.TaskID) AS FirstOfTaskID
FROM tblTasks
GROUP BY tblTasks.ContactID;

appears to work (returns 5 records) but I know from experience that
FirstOfTaskID could return a TaskID from a different record. Whilst it
works with the above data, it does not work with the table from which
this data is sampled although I can't figure out why.

The methods in the above link don't work .
e.g
SELECT Q.ContactID, Q.TaskID
FROM tblTasks AS Q INNER JOIN (SELECT ContactID, Min(Due) As S
FROM tblTasks
GROUP BY ContactID) AS T ON (Q.Due=T.S) AND (Q.ContactId=T.ContactId);

returns 8 rows of ContactID and TaskID
I'd like a query (or nest of queries) to return 5 records (TaskID's 1,
4, 7,8,9) - can anyone help please?

TIA
 

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