dbTalk Databases Forums  

Restricting Records to MAX

comp.databases.ms-access comp.databases.ms-access


Discuss Restricting Records to MAX in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
FireyColin
 
Posts: n/a

Default Restricting Records to MAX - 01-11-2011 , 06:02 AM






I need to use the following query to fill a subform in Access and I
want to to limit the records returned to the latest 'version_no'. It
was all working fine until I added the 'Policy.unique_record_number'
column to the query, after which it began to return all records. How
can I prevent this from happening?

SELECT DISTINCT Policy.unique_record_number, Policy.policy_no,
Max(Policy.version_no) AS MaxOfversion_no, Policy.insured_id,
Organisation.organisation_name, Organisation.organisation_alias FROM
Policy INNER JOIN Organisation ON Policy.insured_id =
Organisation.organisation_id GROUP BY Policy.unique_record_number,
Policy.policy_no, Policy.insured_id, Organisation.organisation_name,
Organisation.organisation_alias;

Reply With Quote
  #2  
Old   
Bob Barrows
 
Posts: n/a

Default Re: Restricting Records to MAX - 01-11-2011 , 07:40 AM






FireyColin wrote:
Quote:
I need to use the following query to fill a subform in Access and I
want to to limit the records returned to the latest 'version_no'. It
was all working fine until I added the 'Policy.unique_record_number'
column to the query, after which it began to return all records. How
can I prevent this from happening?

SELECT DISTINCT Policy.unique_record_number, Policy.policy_no,
Max(Policy.version_no) AS MaxOfversion_no, Policy.insured_id,
Organisation.organisation_name, Organisation.organisation_alias FROM
Policy INNER JOIN Organisation ON Policy.insured_id =
Organisation.organisation_id GROUP BY Policy.unique_record_number,
Policy.policy_no, Policy.insured_id, Organisation.organisation_name,
Organisation.organisation_alias;
By removing the 'Policy.unique_record_number' from the query of course! It's
kind of like the "Doctor, it hurts when I lift my arm" joke ...
;-) ;-)

DISTINCT guarantees the rows will be different, not specific columns in the
rows. To do this, it compares ALL the columns in each row to the columns in
the rest of the rows.

What you really want is the unique_record_number from the row containing the
MaxOfversion_no. To do this will require joining the Policy table to the
results from the query you had before adding 'Policy.unique_record_number'.
The cleanest way to do this is to save that original query, calling it
something like MaxVersionPerInsured or something like that and creating a
new query for your subform like this
SELECT p.unique_record_number, p.policy_no,
MaxOfversion_no, p.insured_id, organisation_name,
organisation_alias FROM Policy AS p JOIN
MaxVersionPerInsured AS q on p.policy_no=q.policy_no
AND p.version_no=MaxOfversion_no AND
p.insured_id=q.insured_id

Reply With Quote
  #3  
Old   
FireyColin
 
Posts: n/a

Default Re: Restricting Records to MAX - 01-11-2011 , 08:39 AM



Many Thanks Bob

It's so easy when you know how isn't it?

Colin

Reply With Quote
  #4  
Old   
Bob Barrows
 
Posts: n/a

Default Re: Restricting Records to MAX - 01-11-2011 , 08:47 AM



FireyColin wrote:
Quote:
Many Thanks Bob

It's so easy when you know how isn't it?

Colin
Posing the question correctly is the first (and usually hardest) part of the
process.

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.