Results 1 to 12 of 12
  1. #1
    Mehvan's Avatar
    Mehvan is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2016
    Posts
    133

    Post Searching at the last two records

    Hello Everyone



    I want to make a query searching at the last two records only for each worker. on condition the last record is less than before. In other words, if the worker received 200 weeks ago and received 300 today, it will not be included in the result of the query. But if received a week ago 300 and today received 200 is displayed. (Search only in last two records)

    for example :

    Ali 200$ 01/02/2018
    Ali 250$ 10/02/2018
    Ali 300$ 19/02/2018
    Ali 200$ 28/02/2018

    Now Ali sent 300$ in 19/02/2018 , it must not show me 200$ in 28/02/2018 , I want to show me only 300$ in 19/02/2018 , if be more than 19/02/2018 it must show me on last record

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,927
    you'll need to use a subquery to find the last two, and your table needs to have a primary key.

    Code:
    SELECT TOP 1 *
    FROM myTable
    WHERE PK=(SELECT TOP 2 PK FROM myTable T WHERE myName=myTable.myName ORDER by MyDate Desc)
    AND myName='Ali' 
    ORDER BY myDate Asc

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    ...except when it was raining on a tuesday on a full moon.

    Q1, get the last record by asking for the MIN(date), select [clientID],Min(date) from tData
    Q2, get the next record by asking for the Min(date) but tData.date > Q1.date
    Q3, put Q1 & Q2 , and tData table in a query , join on clientID, and set Q2.date > Q1.date...

    (this NOT exact sql)
    like select tData.* from tData, Q1,Q2 where tData.ClientID =Q1.clientID = Q2.clientID where Q2.date > Q1.date

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Consider:

    Query to retrieve TOP 2 records per group from table: Top2

    SELECT Table1.ID, Table1.EmpName, Table1.Amount, Table1.DateRcd
    FROM Table1 WHERE ID IN (SELECT TOP 2 ID FROM Table1 AS Dupe
    WHERE Dupe.EmpName=Table1.EmpName ORDER BY Dupe.DateRcd DESC);

    Query to retrieve the TOP 1 record per group from Top2:

    SELECT Top2.ID, Top2.EmpName, Top2.Amount, Top2.DateRcd
    FROM Top2 WHERE ID IN (SELECT TOP 1 ID FROM Top2 AS Dupe
    WHERE Dupe.EmpName = Top2.EmpName ORDER BY Dupe.Amount DESC);
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Mehvan's Avatar
    Mehvan is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2016
    Posts
    133
    I don't want to give me a MIN Date before last record
    maybe I will have so much workers and they will take 4 times salary at the Month , I mean every week they will take part of Salary
    When we use LAST in Query , it will gives us last date he/she takes a part of salary
    but MIN it will gives us which day He/She took less salary in this Year
    it will gives me a mistake result

    I have two Tables :
    1 - Workers ( ID , WorkersName , Salary )
    2 - WorkersDetails : (ID , Amount , Date) , I mean tblDetails will be subQuery for the first Table .

  6. #6
    Mehvan's Avatar
    Mehvan is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2016
    Posts
    133
    Excuse me June7 , but it doesn't give me too my result

  7. #7
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    In your example
    Code:
    Ali  200$   01/02/2018
    Ali  250$   10/02/2018
    Ali  300$   19/02/2018
    Ali  200$   28/02/2018
    the logic , as per ajax's response, is
    Select the top 2 records with the greatest date desc.
    And in that pair, select the top 1 with greatest dollar value.

    Date and Amount are reserved word in Access.
    Good luck

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    And that is the logic applied in my approach according to description in original post.

    If that is not what you want then provide a sample of what the desired output should look like. Perhaps also a larger input sample using several names.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    Mehvan's Avatar
    Mehvan is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2016
    Posts
    133
    I'm sorry for late replied , because I was so busy at work today

    you're bring the second one , and I want one before the last record of that be larger than last

    Ali 200$ 01/02/2018
    Ali 250$ 10/02/2018
    Ali 300$ 19/02/2018
    Ali 200$ 28/02/2018

    in this example , 300$ 19/02/2018 is larger than 200$ 28/02/2018 , it must gives me 300$ 19/02/2018 , not gives me 10/02/2018
    if I give Him 150$ in 05/03/2018 , it must gives me 28/02/2018 and so on .


  10. #10
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,927
    so looks like the code provided in post #2 does what you require, probably the others as well. Try them and let us know how you get on

  11. #11
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    ??? I am not understanding your comment.

    As I posted in post 7
    ========
    The logic is:
    a)Select the top 2 records with the greatest date desc.
    b)And in that pair, select the top 1 with greatest dollar value.

    So using your data
    Code:
    Ali 200$ 01/02/2018
    Ali 250$ 10/02/2018
    Ali 300$ 19/02/2018
    Ali 200$ 28/02/2018
    The latest top 2 dates (a) are
    Code:
    Ali 300$ 19/02/2018
    Ali 200$ 28/02/2018
    The greatest top 1 Dollar Value of this pair (b) is
    Code:
    Ali 300$ 19/02/2018

  12. #12
    Mehvan's Avatar
    Mehvan is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2016
    Posts
    133
    Thanks for everybody it's working now

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

Similar Threads

  1. Searching for records
    By JeRz in forum Programming
    Replies: 5
    Last Post: 08-16-2016, 09:28 AM
  2. searching through records
    By lonesoac0 in forum Access
    Replies: 2
    Last Post: 04-20-2016, 10:30 AM
  3. DAO ADO and Query for searching records
    By TioAdjie in forum Forms
    Replies: 12
    Last Post: 02-20-2014, 08:16 PM
  4. Replies: 10
    Last Post: 01-10-2011, 07:52 PM
  5. Searching records....
    By knightjp in forum Database Design
    Replies: 0
    Last Post: 01-07-2009, 05:20 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