Results 1 to 8 of 8
  1. #1
    jyellis is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Posts
    20

    Top 3 is not working

    Click image for larger version. 

Name:	top 3 not working.JPG 
Views:	8 
Size:	73.3 KB 
ID:	9330



    I'm working in Access 2010. This query does not work. It returns all the records; not the top three in curFreight field
    What am I doing wrong?

  2. #2
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    What are you trying to accomplish with this query? Get the top three values of curFreight regardless of customer by the ShipVIA key?

  3. #3
    jyellis is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Posts
    20
    I'm trying to get the top 3 curfreight within each group of Country/ShipVIa

  4. #4
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    This is the test data I used:

    tblCustomers
    Code:
    pkeyCustomerID  strCountry
    1              Mexico
    2              France
    3              USA
    4              Spain
    tblOrders
    Code:
    pkeyOrderID  fkeyShipVia  curFreight  fkeyCustomerID
    1            1            500         1
    2            2            1000        1
    3            3            1500        1
    4            4            2000        1
    5            1            2500        2
    6            2            2000        2
    7            3            1500        2 
    8            4            1000        2
    This was the result I got as from your query

    Code:
    strCountry  fkeyShipVia  curFreight
    France      4            1000
    France      3            1500
    France      2            2000
    Mexico      1            500
    Mexico      2            1000
    Mexico      3            1500
    This seems to be giving what you are asking for.

  5. #5
    jyellis is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Posts
    20
    Below are three tables...the first is raw data. Second is the sorted data just to help you visualize it....the third the rows highlighted are the desired result. The top 3 recent entries in each group. I hope this helps.
    Click image for larger version. 

Name:	desired results.JPG 
Views:	6 
Size:	106.2 KB 
ID:	9337

  6. #6
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    the IN statement of your query is the problem I think you are only taking the top 3 based on the CUSTOMERID when it needs to be based both on the customerID and on the shippedviaID. The SQL statement in that IN clause would need to include the table that has the pkeyShipVia with a valid link statement and field definitions. (sorry I don't have time to look at it more in depth today)

  7. #7
    peterr89 is offline Novice
    Windows XP Access 2007
    Join Date
    Sep 2012
    Posts
    3
    Hello, get the top three values of curFreight regardless of customer by the ShipVIA key?
    http://www.centplay.com/affiliate/id_139/
    Last edited by peterr89; 10-05-2012 at 01:36 PM.

  8. #8
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Code:
    SELECT tblOrders.fkeyCustomerID, tblCustomers.strCountry, tblShipVia.ShipViaName, tblOrders.curFreight
    FROM (tblOrders LEFT JOIN tblShipVia ON tblOrders.fkeyShipVia = tblShipVia.pkeyShipVia) LEFT JOIN tblCustomers ON tblOrders.fkeyCustomerID = tblCustomers.pkeyCustomerID
    WHERE (((tblOrders.curFreight) In (Select Top 3 [curFreight] From tblOrders Where [fkeyshipvia]=[tblshipvia].[pkeyshipvia] AND [fkeycustomerID] = [tblcustomers].[pkeycustomerid] Order By [curfreight] Desc)))
    ORDER BY tblCustomers.strCountry, tblShipVia.ShipViaName, tblOrders.curFreight DESC;
    I used the same data you showed in three tables

    tblShippedVia
    pkeyShippedVia
    ShipViaName (FedEx, UPS)

    tblCustomers
    pkeyCustomerID
    strCountry (france, england)

    tblOrders
    pkeyOrderID
    fkeyCustomerID
    fkeyShippedVia
    curFreight

    I setup the data to exactly mimic your spreadsheet screen shot and got the results you were after.

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

Similar Threads

  1. Code Not Working
    By Kirsti in forum Programming
    Replies: 3
    Last Post: 03-26-2012, 02:48 PM
  2. IIF Statement not working...
    By LanieB in forum Queries
    Replies: 6
    Last Post: 01-05-2012, 12:55 PM
  3. IIF NULL not working
    By krutoigoga in forum Reports
    Replies: 6
    Last Post: 05-12-2010, 01:17 PM
  4. Working with PDF's
    By Mitch87 in forum Access
    Replies: 1
    Last Post: 02-19-2010, 11:24 AM
  5. Why isn't this working?
    By adiecidue in forum Queries
    Replies: 4
    Last Post: 04-27-2009, 10:29 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