Results 1 to 3 of 3
  1. #1
    JayRab is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Oct 2013
    Posts
    78

    Question Subquery Help! Comparing Dates between rows

    Subqueries... never had to use them yet.

    I am trying to calculate MTTF (mean time to failure). I have a table of equipment events with date/time fields called TimeDelivered. I need to calculate the datediff() between all TimeDelivered fields for each specific piece of equipment.

    So i know I need to use a subquery but not sure how to below is the code for the main query.
    [CODE]SELECT [311_30DAYAVAILABILITYWOKRINGTABLE].Equipment, [311_30DAYAVAILABILITYWOKRINGTABLE].TimeDelivered
    FROM 311_30DAYAVAILABILITYWOKRINGTABLE
    WHERE ((Not ([311_30DAYAVAILABILITYWOKRINGTABLE].TimeDelivered) Is Null))
    ORDER BY [311_30DAYAVAILABILITYWOKRINGTABLE].Equipment, [311_30DAYAVAILABILITYWOKRINGTABLE].TimeDelivered;[CODE]


    Click image for larger version. 

Name:	subqqueryhelp.png 
Views:	9 
Size:	8.3 KB 
ID:	15460

    Now on my subquery I need to create another column that contains the next time delivered for 305(or what ever equipment) so for the above picture there will be another column called NextTimedelivered and on first row it will contain [ 1/28/2014 2:38:16 ] I would then added a calculated field to get that date difference.

    Here is my main query with subquery code added
    Code:
    SELECT [311_30DAYAVAILABILITYWOKRINGTABLE].Equipment, [311_30DAYAVAILABILITYWOKRINGTABLE].TimeDelivered
    (SELECT TOP 1 Dupe.TimeDelivered 
    FROM [311_30DAYAVAILABILITYWOKRINGTABLE] AS Dupe
    WHERE Dupe.Equipment = [311_30DAYAVAILABILITYWOKRINGTABLE].Equipment
    AND Dupe.TimeDelivered > [311_30DAYAVAILABILITYWOKRINGTABLE].TimeDelivered
    ORDER BY Dupe.TimeDelivered ASC, Dupe.Equipment) as NextTimeDelivered
    FROM 311_30DAYAVAILABILITYWOKRINGTABLE
    WHERE ((Not ([311_30DAYAVAILABILITYWOKRINGTABLE].TimeDelivered) Is Null))
    ORDER BY [311_30DAYAVAILABILITYWOKRINGTABLE].Equipment, [311_30DAYAVAILABILITYWOKRINGTABLE].TimeDelivered;
    I get the error - (At Most one record can be returned by this subquery).

    Again never used subquery before so lost on this anyway.

    Anyhelp would be appreciated.

    ps: I know I misspelled some things in my table names

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I did this when I made a mileage dB for my vehicles. but I forget how I did it...

    Pop over the Roger's Access Library and look at
    http://www.rogersaccesslibrary.com/forum/topic445.html

  3. #3
    JayRab is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Oct 2013
    Posts
    78
    Thx for the link Ill take a look at it. I figured out what I was doing wrong on it and got it working correctly. As usual i was making it more complicated then it needed to be.

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

Similar Threads

  1. Comparing dates
    By scoe in forum Queries
    Replies: 8
    Last Post: 11-27-2013, 02:04 AM
  2. Access-comparing Dates
    By espinosaja in forum Access
    Replies: 7
    Last Post: 12-03-2011, 01:14 AM
  3. Comparing dates
    By shanky365 in forum Queries
    Replies: 2
    Last Post: 10-26-2011, 01:28 AM
  4. Function comparing rows
    By Dharmesh in forum Access
    Replies: 1
    Last Post: 10-20-2011, 07:59 AM
  5. Subquery comparing two fields
    By jdcollins in forum Queries
    Replies: 8
    Last Post: 01-17-2011, 02:02 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