Connecting Tech Pros Worldwide Help | Site Map

Returning only min and max dates in Access query

Newbie
 
Join Date: Dec 2006
Posts: 1
#1: Dec 11 '06
Here's a sample recordset in the table I'm looking at:

Code Subcode Date
10051 26 11/1/2006
10051 26 11/25/2006
10051 26 12/2/2006
10052 10/5/2006
10052 11/7/2006
10052 12/1/2006

I want to write a query that will only return rows with the minimum and maximum date for each code/subcode combination. Using the recordset above, the results of this query would look like this:

Code Subcode Date
10051 26 11/1/2006
10051 26 12/2/2006
10052 10/5/2006
10052 12/1/2006

Any advice would be much appreciated. Thanks!
msquared's Avatar
Administrator
 
Join Date: Aug 2006
Location: Dublin, Ireland
Posts: 10,865
#2: Dec 11 '06

re: Returning only min and max dates in Access query


Using a union query would return the format you've suggested.

Expand|Select|Wrap|Line Numbers
  1. SELECT Code, Subcode, Max([Date]) As [Date]
  2. FROM TableName
  3. UNION
  4. SELECT Code, Subcode, Min([Date]) As [Date]
  5. FROM TableName
  6. GROUP BY Code, Subcode;
  7.  
Alternatively, you could return two separate date fields as follows:

Expand|Select|Wrap|Line Numbers
  1. SELECT Code, Subcode, Min([Date]) As MinDate, Max([Date]) As MaxDate
  2. FROM TableName
  3. GROUP BY Code, Subcode;
  4.  

Mary
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,703
#3: Dec 12 '06

re: Returning only min and max dates in Access query


You may want to add :
Expand|Select|Wrap|Line Numbers
  1. ORDER BY Code, Subcode, [Date]
to first version or :
Expand|Select|Wrap|Line Numbers
  1. ORDER BY Code, Subcode
to the second if the order is important. Must be before the ';' though.
Reply