Results 1 to 6 of 6
  1. #1
    NoiCe is offline Novice
    Windows XP Access 2003
    Join Date
    Nov 2008
    Posts
    24

    SQL Syntax Error

    Hi,

    I'm trying to create a query that selects the most recent record from a table containing duplicate records based on Item Code.

    I keep getting a syntax error:

    Code:
    SELECT DUPES.id, DUPES.[Item Code], DUPES.[Date Delivered], DUPES.[Item Description], DUPES.[Received Qty], DUPES.Vendor, DUPES.Comments, DUPES.[Doc #], DUPES.Weight, DUPES.PO, DUPES.[Date Posted to Inventory], DUPES.[Time of Posting], DUPES.[Date Delivered 2], DUPES.[Time of Delivery], DUPES.[0-4 Hours Posting Delay], DUPES.[4-8 Hours Posting Delay], DUPES.[8-24 Hours Posting Delay], DUPES.[Over 24 Hours Posting Delay]
    FROM DUPES
    WHERE ((DUPES.[Date Delivered]<=#3/31/2011#))
    (SELECT MAX(DUPES.[Date Delivered])
    FROM DUPES AS X
    WHERE X.[Item Code] = DUPES.[Item Code]);
    The table is something like this:

    ITEM CODE|Date Received|Info1|Qty
    Item A|03/01/2011|This is item A|300
    Item A|03/28/2011|This is item A|200
    Item A|02/27/2010|This is item A|3
    Item B|03/03/2011|This is item B|200


    Item B|03/27/2011|This is item B|2

    The results should be:

    Item A|03/28/2011|This is item A|200
    Item B|03/27/2011|This is item B|2

    I appreciate anyone's help!!! Thank you

  2. #2
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    In the WHERE clause, shouldn't there be an operator between the conditions??


    Code:
    WHERE ((DUPES.[Date Delivered]<=#3/31/2011#)) AND
    (SELECT MAX(DUPES.[Date Delivered])
    FROM DUPES AS X
    WHERE X.[Item Code] = DUPES.[Item Code]);
    or maybe

    Code:
    WHERE (SELECT MAX(DUPES.[Date Delivered])
    FROM DUPES AS X
    WHERE X.[Item Code] = DUPES.[Item Code] AND X.[Date Delivered]<=#3/31/2011#);

  3. #3
    NoiCe is offline Novice
    Windows XP Access 2007
    Join Date
    Nov 2008
    Posts
    24
    Both ways do not provide me the end result that I'm looking for. They basically still give me all of my original records back. I'm looking to obtain only the records that have the closest date to today without duplicates. De-duping on Item Code.

    Any thoughts?

  4. #4
    Rixxe is offline Blessed Bear
    Windows 7 Access 2007
    Join Date
    Sep 2010
    Location
    London (Essex ... yep!)
    Posts
    184
    I'd look into Group By/Distinct, and something like:

    WHERE [YourDateField] between Today() and [AnotherDateYouWant]

    Or obviously you can use MAX, and TOP to get the top 50 records say with the max date.

    Good Luck

  5. #5
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Quote Originally Posted by NoiCe View Post
    Both ways do not provide me the end result that I'm looking for. <snip>
    Any thoughts?
    But you don't have the syntax error any more........


    I tried to make a table to use in a query, but the sub query has [Date Delivered] that is not in the example data.

    (Also, having spaces and special characters in the field names doesn't help)

  6. #6
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    you should start here:

    Code:
    select code, max(received)
    
    from table
    then a second could be this:

    Code:
    select all fields from table
    
    where code in (select qry1.code from qry1) and 
    
    received in (select qry1.received from qry1)
    there's nothing wrong with using stacked queries.

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

Similar Threads

  1. Need help with Syntax Error
    By jgelpi16 in forum Programming
    Replies: 2
    Last Post: 02-04-2011, 08:34 AM
  2. Incomplete Syntax Clause (syntax error)
    By ajetrumpet in forum Programming
    Replies: 4
    Last Post: 09-11-2010, 10:47 AM
  3. Syntax error
    By smikkelsen in forum Access
    Replies: 6
    Last Post: 04-28-2010, 09:38 AM
  4. Syntax Error
    By KLynch0803 in forum Programming
    Replies: 11
    Last Post: 02-04-2010, 01:45 AM
  5. sql syntax error
    By SMAlvarez in forum Access
    Replies: 1
    Last Post: 03-12-2009, 09:43 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