dbTalk Databases Forums  

how to max record based on two fields

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


Discuss how to max record based on two fields in the comp.databases.ms-access forum.



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

Default how to max record based on two fields - 11-12-2011 , 08:04 AM






I've got a table with fields bookingId, quoteId, bookingDate
I want to get the max(bookingDate) for the max(bookingId) for a given
quoteId

select bookingId, max(bookingDate) from table where quoteId = 1 group
by bookingId
gives me the max date for each bookingId

select quoteId, max(bookingId), max(bookingDate) from table where
quoteId = 1 group by quoteId
gives me the max bookingId and the max date, but not the max date of
the max bookingId

do I need two queries to do this ?

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

Default Re: how to max record based on two fields - 11-12-2011 , 10:07 AM






Roger wrote:
Quote:
I've got a table with fields bookingId, quoteId, bookingDate
I want to get the max(bookingDate) for the max(bookingId) for a given
quoteId

select bookingId, max(bookingDate) from table where quoteId = 1 group
by bookingId
gives me the max date for each bookingId

select quoteId, max(bookingId), max(bookingDate) from table where
quoteId = 1 group by quoteId
gives me the max bookingId and the max date, but not the max date of
the max bookingId

do I need two queries to do this ?
Yes. One query will be in the FROM clause of the other, joined to the table.
Create a saved query that gives you the max booking id for each quoteid.
Then create a new query that joins the table to the saved query using
quoteid and booking id-maxbookingid.

Reply With Quote
  #3  
Old   
Kaj Julius
 
Posts: n/a

Default Re: how to max record based on two fields - 11-13-2011 , 12:10 PM



"Bob Barrows" <reb01501 (AT) NOSPAMyahoo (DOT) com> skrev i en meddelelse
news:j9m5jb$tli$1 (AT) dont-email (DOT) me...
Quote:
Roger wrote:
I've got a table with fields bookingId, quoteId, bookingDate
I want to get the max(bookingDate) for the max(bookingId) for a given
quoteId

select bookingId, max(bookingDate) from table where quoteId = 1 group
by bookingId
gives me the max date for each bookingId

select quoteId, max(bookingId), max(bookingDate) from table where
quoteId = 1 group by quoteId
gives me the max bookingId and the max date, but not the max date of
the max bookingId

do I need two queries to do this ?
Yes. One query will be in the FROM clause of the other, joined to the
table.
Create a saved query that gives you the max booking id for each quoteid.
Then create a new query that joins the table to the saved query using
quoteid and booking id-maxbookingid.

Alternatively you could use a subselect:

select quoteid, bookingid, max(bookingdate) as bookingdate
from yourtable as t1
where bookingid = (select max(bookingid) from yourtable where
quoteid=t1.quoteid)
group by quoteid, bookingid

However, subselect have a bad reputation for being slow and thus isn't
recommended with big tables.

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.