Results 1 to 6 of 6
  1. #1
    ramirezx@ddmfg.com is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    40

    MS Access subtract previous record from current record

    Hi,

    I'm trying to run a query and used this code, how ever the first two show 0. Not sure what's causing it. I hope someone can help

    -----------------
    SELECT tblData.Date,
    tblData.[Port MDE End],
    [Port MDE End]-
    Nz(
    (Select Top 1
    [Port MDE End]
    From
    tblData As T


    Where
    (T.Date <= tblData.Date And T.[Port MDE End] < tblData.[Port MDE End] )
    Or
    (T.Date < tblData.Date And T.[Port MDE End] <= tblData.[Port MDE End] )
    Order By
    T.Date Desc,
    T.[Port MDE End] Desc),
    [Port MDE End]) AS [Total Hours]

    FROM
    tblData;
    -----------------
    this is what I get:
    Date Port MDE End Total Hours
    1/6/2023 896 0
    1/7/2023 2103 0
    1/7/2023 3220 1117
    1/8/2023 4218 998
    1/8/2023 5435 1217
    1/9/2023 6472 1037

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,570
    That output does not match the SQL?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,941
    Based on the data provided in the first two columns,
    Click image for larger version. 

Name:	image_2023-05-18_001935998.png 
Views:	24 
Size:	6.5 KB 
ID:	50255


    the query you provided works. So the implication is there is data you are not showing
    Click image for larger version. 

Name:	image_2023-05-18_001729876.png 
Views:	24 
Size:	8.6 KB 
ID:	50254

    So show your source data

  4. #4
    ramirezx@ddmfg.com is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    40
    Hi,

    I recopied the code from my query:

    -----------------------------------------------------------------------------------------------------------

    SELECT tblData.Date, tblData.[Port MDE End], [Port MDE End]-Nz((Select Top 1
    [Port MDE End]
    From
    tblData As T
    Where
    (T.Date <= tblData.Date And T.[Port MDE End] < tblData.[Port MDE End] )
    Or
    (T.Date < tblData.Date And T.[Port MDE End] <= tblData.[Port MDE End] )
    Order By
    T.Date Desc,
    T.[Port MDE End] Desc),[Port MDE End]) AS [Total Hours]
    FROM tblData;

  5. #5
    ramirezx@ddmfg.com is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    40
    Hi,

    Here is the table where the data is stored:

    Date Port MDE End
    1/6/2023 896
    1/7/2023 2103
    1/7/2023 3220
    1/8/2023 4218
    1/8/2023 5435
    1/9/2023 6472

    Thanks!

  6. #6
    ramirezx@ddmfg.com is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    40
    Hi everyone,

    Thanks for you help, I found out what the problem was. the Port MDE End field data type was Short Text, when I changed it to Number is working now!

    Thanks again everyone for you help!

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

Similar Threads

  1. Replies: 4
    Last Post: 10-13-2021, 01:14 PM
  2. Replies: 3
    Last Post: 11-20-2018, 01:34 AM
  3. Replies: 7
    Last Post: 10-06-2014, 01:28 PM
  4. Replies: 2
    Last Post: 03-05-2014, 05:37 PM
  5. Subtract From Previous Record Using a Date
    By txrules in forum Queries
    Replies: 1
    Last Post: 12-30-2010, 02:10 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