Results 1 to 6 of 6

Thread: Record grouping problem [Resolved]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2000
    Location
    Sydney Australia
    Posts
    476

    Record grouping problem [Resolved]

    Hi team

    I have a table that holds contract details for travel agents and customers. The table contains details of expired contract as well as current contract. I can tell which contract is current by looking at the ContractExpireDate.

    I get the latest contract by grouping on the agent ID and customerID and get the Max(ContractExpireDate).

    This works fine. The problem is that I also want to get the ContactID of the Max(ContractExpireDate) record. Max(ContactID) is not guaranteed to be from the same record as Max(ContractExpireDate).

    So my question is, can I do the grouping so that the ContractID I get back is guaranteed to be from the same record as the ContractExpireDate?

    Thanks

    FW
    Last edited by freewilly; Jan 18th, 2004 at 05:30 PM.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    What SQL Query are you using?

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2000
    Location
    Sydney Australia
    Posts
    476
    Hi mendhak

    Heres a db with the data and the query. I need to get the ContractID that corresponds to the record with the highest ContractDate.

    Thanks

    FW
    Attached Files Attached Files

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Erm... Doesn't Access let you type the SQL Query anywhere?

  5. #5
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    Try this:
    Code:
    SELECT X.CustomerID, T.ContractID, X.MaxDate
    FROM (SELECT CustomerID, MAX(DateEnd) AS MaxDate FROM tblContract GROUP BY CustomerID) AS X
    INNER JOIN tblContract AS T
    ON  X.CustomerID = T.CustomerID
    WHERE T.DateEnd = X.MaxDate
    "It's cold gin time again ..."

    Check out my website here.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2000
    Location
    Sydney Australia
    Posts
    476
    Thanks Bruce

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width