Results 1 to 3 of 3
  1. #1
    kagoodwin13 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    181

    Nested And/Or query

    Hi all. I've made some pretty significant progress on my database, thanks to all the help I've received here. I'm stuck on something right now, and I'm wondering if I can get a quick fix.



    I am pulling trucking data to create queries that will calculate trucking cost per mile. Sometimes the trucks run in a round-trip (RT), and other times they only run one way (1-Way or 1W). This formula also includes number of each trip per week to create a weighted average. I am also creating a query for each year so that I can create a summarized report for round-trip and one-way cost per mile per year.

    I'd like to query for data on trucking lanes with round-trips over 200 miles, or one-ways over 100 miles. This is so I can eliminate small runs that would unfairly skew cost-per-mile results. I also need to filter the data by year.

    So far I have this query:

    Code:
    SELECT [Truckload].[RT Miles], [Truckload].[RT Rates], [Truckload].[RT per Week], [Truckload].[1-Way Miles], [Truckload].[1W Rates], [Truckload].[1W Per Week], [Truckload].[Start Date], [Truckload].[End Date]
    FROM [Truckload]
    WHERE ((([Truckload].[RT Miles])>200)) AND ((([Truckload].[1-Way Miles])>100) AND (([Truckload].[Start Date])<#12/31/2010#) AND (([Truckload].[End Date])>#1/1/2010#));
    The tricky part is that sometimes there is a shipping lane with both round-trip and one-way rates, and other times they only have one of each. Therefore I'd like to capture [RT Miles]>200 OR [1-Way Miles]>100. When I change this:

    Code:
    WHERE ((([Truckload].[RT Miles])>200)) AND ((([Truckload].[1-Way Miles])>100)
    to this:

    Code:
    WHERE ((([Truckload].[RT Miles])>200)) OR ((([Truckload].[1-Way Miles])>100)
    the query returns results that are outside of the date range.

    I've tried switching around the two halves of the WHERE statement as well so that it reads like this:
    Code:
    SELECT [Truckload].[RT Miles], [Truckload].[RT Rates], [Truckload].[RT per Week], [Truckload].[1-Way Miles], [Truckload].[1W Rates], [Truckload].[1W Per Week], [Truckload].[Start Date], [Truckload].[End Date]
    FROM [Truckload]
    WHERE (([Truckload].[Start Date])<#12/31/2010#) AND (([Truckload].[End Date])>#1/1/2010#)) AND ((([Truckload].[RT Miles])>200)) OR ((([Truckload].[1-Way Miles])>100);
    but I get a syntax error that I can't solve then.

    Any suggestions?

  2. #2
    kagoodwin13 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    181
    Solved:

    Code:
    WHERE (([Truckload].[Start Date])<#12/31/2010#) AND (([Truckload].[End Date])>#1/1/2010#) AND ([Truckload].[RT Miles]>200)
    
    OR ((([Truckload].[Start Date])<#12/31/2010#) AND ([Truckload].[End Date])>#1/1/2010#) AND ([Truckload].[1-Way Miles]>100);

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    This thread is marked as solved; is it? If not, you need to parentheses the items in the WHERE clause when you mix AND and OR:

    A And (B Or C)
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. nested query: avg, count, group by
    By gap in forum Queries
    Replies: 2
    Last Post: 07-05-2011, 07:48 AM
  2. Nested SQL Query
    By springboardjg in forum Queries
    Replies: 5
    Last Post: 05-08-2011, 05:01 PM
  3. Another Nested IIF Query problem
    By Brian Collins in forum Queries
    Replies: 8
    Last Post: 10-22-2010, 10:12 AM
  4. Nested Query Question Access 2007
    By databased in forum Queries
    Replies: 5
    Last Post: 10-15-2010, 07:22 AM
  5. Nested IIf query will not work
    By ddog171 in forum Queries
    Replies: 1
    Last Post: 06-20-2006, 02:03 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