Results 1 to 12 of 12
  1. #1
    rajncajn is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Mar 2015
    Posts
    6

    Query finding earlist date among common numbers

    I have a query set up to pull all data from a table that has a "SAT" result in the STATUS column. The results or columns I am pulling are the STATUS, TEST NUMBER, TEST DATE and TEST IDENTIFICATION NUMBER. The test numbers are often duplicated many times and what I am trying to find with the query is only the earliest dates the I recieved a SAT result for each test number.

    This is an example of the results I'm getting with my query:


    STATUS TEST NO TEST DATE TEST IDENT #
    sat 45-1 5/5/13 1
    sat 45-1 9/10/13 2
    sat 45-1 10/8/14 3
    sat 48-3 1/2/15 4
    sat 48-3 5/8/15 5
    sat 48-3 7/30/15 6
    sat 48-3 8/8/13 7
    sat 92-8 7/2/12 8
    sat 92-8 11/11/15 9
    sat 92-8 1/8/12 10

    This is an example of what I actually need:
    STATUS TEST NO TEST DATE TEST IDENT #
    sat 45-1 5/5/13 1
    sat 48-3 8/8/13 7
    sat 92-8 1/8/12 10

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850
    Try (untested)
    Code:
    SELECT  *
    FROM Table
    WHERE Table.status ="sat"  and 
    testdate = (select Min(W.[testdate])   from  Table as W)

  3. #3
    rajncajn is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Mar 2015
    Posts
    6
    Quote Originally Posted by orange View Post
    Try (untested)
    Code:
    SELECT  *
    FROM Table
    WHERE Table.status ="sat"  and 
    testdate = (select Min(W.[testdate])   from  Table as W)
    It's giving me a Syntax error for missing operator in the last two lines.

    Here is my existing code:

    Code:
    SELECT ProcedureLog.Status, ProcedureLog.Test No, ProcedureLog.TestDate, ProcedureLog.Test Ident #
    FROM ProcedureLog
    WHERE (((ProcedureLog.Status)="SAT"))
    ORDER BY ProcedureLog.Test No;

  4. #4
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    If you have spaces or special characters like "#" in field names, you need to enclose the field name in square brackets, i.e.
    Code:
    SELECT ProcedureLog.Status, ProcedureLog.[Test No], ProcedureLog.TestDate, ProcedureLog.[Test Ident #]
    FROM ProcedureLog
    WHERE (((ProcedureLog.Status)="SAT"))
    ORDER BY ProcedureLog.[Test No];

  5. #5
    rajncajn is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Mar 2015
    Posts
    6
    Quote Originally Posted by JoeM View Post
    If you have spaces or special characters like "#" in field names, you need to enclose the field name in square brackets, i.e.
    Code:
    SELECT ProcedureLog.Status, ProcedureLog.[Test No], ProcedureLog.TestDate, ProcedureLog.[Test Ident #]
    FROM ProcedureLog
    WHERE (((ProcedureLog.Status)="SAT"))
    ORDER BY ProcedureLog.[Test No];
    While helpful it doesn't answer the original question or correct the syntax error. The actual header text in my table is TWR_NO, but in order to have it make sense for the purpose of this thread I changed it to Test Ident #. Where the syntax error is coming from is where I'm trying to incorporate the change orange suggested. The actual code reads:

    Code:
    SELECT ProcedureLog.Status, ProcedureLog.TP_No, ProcedureLog.TestDate, ProcedureLog.TWR_No
    FROM ProcedureLog
    WHERE (((ProcedureLog.Status)="PSAT"))
    ORDER BY ProcedureLog.TP_No;
    This is what I'm trying to change it to per the suggested code:

    Code:
    SELECT ProcedureLog.Status, ProcedureLog.TP_No, ProcedureLog.TestDate, ProcedureLog.TWR_No
    FROM ProcedureLog
    WHERE (((ProcedureLog.Status)="PSAT")) and
    testdate = (select Min(W.[testdate])   from  Table as W)
    ORDER BY ProcedureLog.TP_No;

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850
    You will need a Group BY line before the Order By
    Here is a query with similar construct (it uses max, not Min)
    Code:
    SELECT tblStatsProdSales.ProdCode
    , Max(tblStatsProdSales.Sales) AS MaxOfSales
    , tblStatsProdSales.Region
    FROM tblStatsProdSales
    WHERE (((tblStatsProdSales.[sales])=
               (select Max( sales) from  tblStatsProdSales as x
                where x.region = tblStatsProdSales.region)))
    GROUP BY ProdCode, Region;

  7. #7
    rajncajn is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Mar 2015
    Posts
    6
    Quote Originally Posted by orange View Post
    You will need a Group BY line before the Order By
    Here is a query with similar construct (it uses max, not Min)
    Code:
    SELECT tblStatsProdSales.ProdCode
    , Max(tblStatsProdSales.Sales) AS MaxOfSales
    , tblStatsProdSales.Region
    FROM tblStatsProdSales
    WHERE (((tblStatsProdSales.[sales])=
               (select Max( sales) from  tblStatsProdSales as x
                where x.region = tblStatsProdSales.region)))
    GROUP BY ProdCode, Region;
    As much as I hate to be a pain I just can't seem to get this to work. Constantly getting errors trying to plug & play the info in my database into your example.

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

  9. #9
    rajncajn is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Mar 2015
    Posts
    6
    Quote Originally Posted by orange View Post
    Post a copy of the database --- compact and repair , then zip
    Unfortunately I cannot because it is full of propietary information.

  10. #10
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850
    Can you export some sample data to csv and post the data?
    We can offer much if we can not see the table design, the data and the exact query you are using.
    What can you supply to readers?

  11. #11
    rajncajn is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Mar 2015
    Posts
    6
    Quote Originally Posted by orange View Post
    Can you export some sample data to csv and post the data?
    We can offer much if we can not see the table design, the data and the exact query you are using.
    What can you supply to readers?
    Thankfully was able to find a solution earlier today with some additional help. I still want to thank you for trying to help me with this. You offered a great starting point which ultimately got my question answered. For reference, here is what I ended up with:

    Code:
    SELECT plog.Status, plog.TP_No, plog.TestDate, Min(plog.TWR_No) AS MinOfTWR_No
    FROM (SELECT TP_No, min(TestDate) AS mindate FROM ProcedureLog WHERE Status='PSAT'
    GROUP BY TP_No)  AS blog INNER JOIN ProcedureLog AS plog ON (blog.mindate=plog.TestDate) AND (blog.TP_No=plog.TP_no) GROUP BY plog.Status, plog.TP_No, plog.TestDate
    HAVING (((plog.Status)='PSAT'))
    ORDER BY plog.TP_No;

  12. #12
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850

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

Similar Threads

  1. Replies: 1
    Last Post: 01-10-2013, 08:33 AM
  2. Replies: 2
    Last Post: 12-08-2012, 10:01 AM
  3. Query finding sales by date
    By v!ctor in forum Queries
    Replies: 9
    Last Post: 10-14-2012, 04:41 PM
  4. Replies: 2
    Last Post: 06-11-2011, 10:39 PM
  5. Replies: 1
    Last Post: 03-05-2010, 12:27 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