Results 1 to 7 of 7
  1. #1
    scratchmb is offline Novice
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    6

    Query that sums a field based on 2 tables

    I have 2 tables the first table "tblpayrollmiles" has the fields "Miles" "ProcessDate" , the second table "Accidents" has the fields "IncidentDate" "Rate".



    I need to sum the "Miles" based on the "ProcessDate" greater than the last "IncidentDate" that the "Rate" ="1" Or "3" . I have been able to do that, however if there is no "IncidentDate" with "Rate"="1" Or "3" , I get no value.

    Please help!

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Post the query SQL statement for analysis.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    scratchmb is offline Novice
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    6
    SELECT DISTINCTROW tblPayrollMiles.[Driver code], tblPayrollMiles.Miles, Last(tblAccidents.DateofIncident) AS LastOfDateofIncident, tblPayrollMiles.ProcessDate
    FROM tblAccidents INNER JOIN tblPayrollMiles ON tblAccidents.[Driver code] = tblPayrollMiles.[Driver code]
    WHERE (((tblAccidents.RATE) Like "1" Or (tblAccidents.RATE)="3"))
    GROUP BY tblPayrollMiles.[Driver code], tblPayrollMiles.Miles, tblPayrollMiles.ProcessDate
    HAVING (((Last(tblPayrollMiles.ProcessDate))>Max([tblAccidents].[DateofIncident])));

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    The query provided some clarifying info, such as the correct names of table and fields and what the linking fields are, however, I am still unclear about what the issue is.

    Do you mean rate for an incident date is neither 1 nor 3? What would it be otherwise? If no records match this criteria then the no results output is correct.

    I think you will have to show some sample source data and the desired output. Could even provide the project.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    scratchmb is offline Novice
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    6
    basically what i'm trying to do is track consecutive safe miles for a particular driver based on either total miles if they have had no preventable accidents, or [Miles] since the last preventable accident. [RATE] can be 1, 2, 3, 4 - 2 or 4 doesn't affect consecutive safe miles while 1 or 3 would reset consecutive miles to 0 from the date of the accident.

    For instance a driver may have 1,000,000 total miles since he started driving, however if he had a preventable accident 1 year ago, he would have only 125,000 miles since his last preventable accident. i need the query to show the total miles since he had his last preventable or if he has no preventable accidents it would show total miles

    the table with the miles is updated every week [ProcessDate] the table with the accidents is updated at the time the driver has the accident [IncidentDate]

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Try:

    SELECT tblpayrollmiles.[Driver code], Sum(tblpayrollmiles.Miles) AS SumOfMiles
    FROM
    (SELECT tblAccidents.[Driver code], Max(tblAccidents.DateOfIncident) AS MaxOfDateOfIncident
    FROM tblAccidents
    WHERE (((tblAccidents.Rate)="1" Or (tblAccidents.Rate)="3"))
    GROUP BY tblAccidents.[Driver code]) AS Query1
    RIGHT JOIN tblpayrollmiles ON Query1.[Driver code] = tblpayrollmiles.[Driver code]
    WHERE (((tblpayrollmiles.ProcessDate)>Nz([MaxOfDateOfIncident],#1/1/2000#)))
    GROUP BY tblpayrollmiles.[Driver code];
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    scratchmb is offline Novice
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    6
    I'm going through my data but at first glance that looks like it worked exactly as i needed. I appreciate your help.

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

Similar Threads

  1. Create tables based on field value
    By fpmsi in forum Programming
    Replies: 5
    Last Post: 11-17-2011, 12:42 PM
  2. Replies: 1
    Last Post: 08-31-2011, 04:03 PM
  3. Changing tables based on field date
    By dssrun in forum Programming
    Replies: 6
    Last Post: 07-10-2011, 10:17 AM
  4. Replies: 2
    Last Post: 07-07-2011, 08:25 AM
  5. Replies: 6
    Last Post: 06-08-2011, 05:00 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