Results 1 to 8 of 8
  1. #1
    TriFith is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Aug 2013
    Location
    Czech republic, Prague
    Posts
    37

    Selecting last date for every record from 2nd table

    Hello everybody,
    May I ask you for help with sql query what I canīt even name?

    I will try to describe my issue...
    I have two tables (tblCustomers and tblPMs). TblPMs contains foreign key to tblCustomers and it contains multiple records for every customer.
    My goal is to create query what will pick the last record (regarding the EffectiveDay column in tblPMs) for every customer stored in tblCustomers.

    Attached is the stucture of tables. I believe there should be some simple way how to do that, I just canīt find the right way

    Thanks for advice


    TriFith
    Attached Thumbnails Attached Thumbnails qry.PNG  

  2. #2
    JamesDeckert is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Jul 2015
    Location
    Salina, KS
    Posts
    262
    sort the query by EffectiveDay descending. Then do a top 1 (ribbon has design>query setup>'return', enter 1)

  3. #3
    TriFith is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Aug 2013
    Location
    Czech republic, Prague
    Posts
    37
    Quote Originally Posted by JamesDeckert View Post
    sort the query by EffectiveDay descending. Then do a top 1 (ribbon has design>query setup>'return', enter 1)
    Hi,

    thanks for your reply... This will gime me only one record with last EffectiveDate and customer related to this record. That is pretty primitive query.
    My issue is to get records for all customers with the last EffectiveDate.
    Iīve heard there is a possibility to set sub query as a counted field, but it means there will be too many sql querry running and it will take extremly long to get results...

  4. #4
    JamesDeckert is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Jul 2015
    Location
    Salina, KS
    Posts
    262
    What about clicking the totals button then group by ID.
    Seems like that would return the top 1 for each ID.

  5. #5
    TriFith is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Aug 2013
    Location
    Czech republic, Prague
    Posts
    37
    Quote Originally Posted by JamesDeckert View Post
    What about clicking the totals button then group by ID.
    Seems like that would return the top 1 for each ID.

    Can you write the sql code please?
    Firstly I think directive SELECT TOP xx is performed before GROUP directive, so I still have only 1 record for this query.
    Also If I will use aggregate function, I have to use it on all fields. I canīt use GROUP BY only for one field...

    I have tried this code and it will give me only one record as a result.
    Code:
    SELECT TOP 1
       tblCustomers.Customer,
       tblPMs.EffectiveDate,
       tblCustomers.Interval,
       tblCustomers.Remarks
    FROM
       tblCustomers INNER JOIN tblPMs ON tblCustomers.ID = tblPMs.CustomerID
    GROUP BY
       tblCustomers.Customer,
       tblPMs.EffectiveDate,
       tblCustomers.Interval,
       tblCustomers.Remarks
    ORDER BY
       tblPMs.EffectiveDate DESC;

  6. #6
    InsuranceGuy is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Aug 2015
    Location
    Colorado
    Posts
    126
    This was a fun logic puzzle when I first encountered it. I like the two query solution because it's so easy to remember.

    Query1:
    Code:
    Select CustomerID, max(EffectiveDate) MaxEffectiveDate from tblPMs
    Query2:
    Code:
    Select tblCustomers.*, tblPMs.* from 
    tblCustomers, tblPMs, Query1
    where
    tblCustomers.ID=tblPMs.CustomerID
    and tblCustomers.ID = Query1.CustomerID
    and tblPMs.EffectiveDate = Query1.MaxEffectiveDate

  7. #7
    TriFith is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Aug 2013
    Location
    Czech republic, Prague
    Posts
    37
    Quote Originally Posted by InsuranceGuy View Post
    This was a fun logic puzzle when I first encountered it. I like the two query solution because it's so easy to remember.

    Query1:
    Code:
    Select CustomerID, max(EffectiveDate) MaxEffectiveDate from tblPMs
    Query2:
    Code:
    Select tblCustomers.*, tblPMs.* from 
    tblCustomers, tblPMs, Query1
    where
    tblCustomers.ID=tblPMs.CustomerID
    and tblCustomers.ID = Query1.CustomerID
    and tblPMs.EffectiveDate = Query1.MaxEffectiveDate

    Unfortunatelly Query1 canīt work
    CustomerID is not aggregated by some function

    But this query guided me to the right solution... Itīs ridiculously simple :-D :-D
    Following query will pick max EffectiveDate for every CustomerID.
    Code:
    SELECT tblPMs.CustomerID, Max(tblPMs.EffectiveDate) AS MaxOfEffectiveDateFROM tblPMs
    GROUP BY tblPMs.CustomerID;

  8. #8
    InsuranceGuy is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Aug 2015
    Location
    Colorado
    Posts
    126
    Quote Originally Posted by TriFith View Post
    But this query guided me to the right solution... Itīs ridiculously simple :-D :-D
    Following query will pick max EffectiveDate for every CustomerID.
    Code:
    SELECT tblPMs.CustomerID, Max(tblPMs.EffectiveDate) AS MaxOfEffectiveDateFROM tblPMs
    GROUP BY tblPMs.CustomerID;
    You got it! Sorry for the miss on my part.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Reports by selecting a date range
    By keiath in forum Reports
    Replies: 10
    Last Post: 01-22-2014, 08:24 AM
  2. Selecting Year to date
    By lugnutmonkey in forum Queries
    Replies: 3
    Last Post: 01-15-2013, 08:48 AM
  3. Selecting record in foreign table using VBA
    By Jmeyering in forum Programming
    Replies: 3
    Last Post: 11-12-2012, 04:29 PM
  4. Replies: 9
    Last Post: 09-16-2011, 03:52 PM
  5. Selecting a str Date
    By benjimillard in forum Queries
    Replies: 1
    Last Post: 06-24-2011, 02:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums