Results 1 to 8 of 8
  1. #1
    tplee is offline Novice
    Windows XP Access 2007
    Join Date
    Nov 2011
    Posts
    6

    Help with most recent 2 weeks query.

    Hello,

    I'm trying to run a query that returns data for the most recent 2 weeks.

    For example the table looks something like this:

    Attachment 5364

    The table is constantly updated so I want the query to return the most recent 2 weeks of data, otherwise i would just type in the specific criteria. So for this example it would return the data for WE ending 12/03/2011 and 11/25/2011. And so if the table was updated with the most recent data for WE ending 12/15/2011 it would return weeks 12/15/2011 and 12/03/2011. Keep in mind im not good with sql. I do everything in design mode.

    Thanks in advance

  2. #2
    orange's Avatar
    orange is online now Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,746
    You will need a query along these lines:substitute your actual table name for yourtable

    Code:
    Select student
    ,[test score]
    ,state
    ,[week ending date] from YourTable 
    where [week ending date] in
         (SELECT TOP 2 [week ending date]
           From YourTable
    ORDER BY [week ending date] DESC)
    You could add an Order By [Test Score] DESC; if you wanted the results in Highest score to lowest score order for all scores during the last two week period.
    You should avoid special characters and spaces in your field and object names.
    Last edited by orange; 11-30-2011 at 08:13 PM. Reason: Corrected the SQL

  3. #3
    alansidman's Avatar
    alansidman is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529
    Orange:
    I did something similar but with the TOP 2, if it there is more than one record for that week, it does not bring back two weeks but the two records from that week. Not sure how to remedy this for the OP. I changed TOP 2 to TOP 3 to get both weeks. But if there are only one per week, this will return three records.

    Alan

  4. #4
    orange's Avatar
    orange is online now Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,746
    Alan,

    I just get the weekending date in the sub query, so all records with either of the 2 week ending dates should be selected.

    If there were 100 records with either of these week ending dates, all should be selected. Do you have a test set up-- I don't.

  5. #5
    alansidman's Avatar
    alansidman is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529
    I set up a test but now it is only giving me the November Date , but not the December Date. Very mysterious. I'll post it so you can see.

  6. #6
    orange's Avatar
    orange is online now Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,746
    I have acc 2003 not 2007 or 2010. Can't use an accdb.

  7. #7
    alansidman's Avatar
    alansidman is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529
    I realized after I posted. Reposted as 2003 file. My bad.

  8. #8
    orange's Avatar
    orange is online now Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,746
    Thanks Alan.

    I think this solves it -- needs the Group by (to get the 2 ) and DESC (to get the latest )

    SELECT Sheet1.student, Sheet1.testscore, Sheet1.state, Sheet1.WEdate
    FROM Sheet1
    WHERE Sheet1.WEdate In (SELECT TOP 2 [WEdate]
    From Sheet1
    Group by WEDate
    ORDER BY Sheet1.WEdate DESC
    );

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

Similar Threads

  1. Replies: 3
    Last Post: 11-22-2011, 11:06 AM
  2. Most recent data in query
    By LisaEllen in forum Queries
    Replies: 4
    Last Post: 09-14-2011, 09:20 PM
  3. Using an update query, but updating +2 weeks...
    By AudiA4_20T in forum Queries
    Replies: 2
    Last Post: 08-02-2011, 07:12 AM
  4. Date Add 26 weeks minus 1 day
    By ker9 in forum Queries
    Replies: 3
    Last Post: 07-26-2011, 01:20 PM
  5. Replies: 4
    Last Post: 05-04-2010, 03:33 PM

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