What are you trying to accomplish with this query? Get the top three values of curFreight regardless of customer by the ShipVIA key?
I'm trying to get the top 3 curfreight within each group of Country/ShipVIa
This is the test data I used:
tblCustomers
tblOrdersCode:pkeyCustomerID strCountry 1 Mexico 2 France 3 USA 4 Spain
This was the result I got as from your queryCode: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 seems to be giving what you are asking for.Code:strCountry fkeyShipVia curFreight France 4 1000 France 3 1500 France 2 2000 Mexico 1 500 Mexico 2 1000 Mexico 3 1500
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)
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.
I used the same data you showed in three tablesCode: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;
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.