Results 1 to 8 of 8
  1. #1
    Ivan_CRO is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2021
    Posts
    10

    Query for subtracting

    Hi, I have one question about access query that I don't know how to solve. I am sending a picture as an example with a couple of data, on which everything is explained.

    I have a table where I get the gas meter condition for an individual customer every day, and now I need to calculate the consumption that the customer had in a particular day. And that is actually state of the meter from the observed day - state of the meter from the previous day.



    In fact, I need a query that would "subtract" my rows in one column (METER_CONDITION) from day to day and display them as a result.

    Click image for larger version. 

Name:	image_2021-11-26_143051.png 
Views:	15 
Size:	166.2 KB 
ID:	46763

  2. #2
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    To compare a value in one row with another row value you need a subquery. Topic covered here, and in fact, is about meters (see 'get the value in another record' )
    http://allenbrowne.com/subquery-01.html
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    Ivan_CRO is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2021
    Posts
    10
    Quote Originally Posted by Micron View Post
    To compare a value in one row with another row value you need a subquery. Topic covered here, and in fact, is about meters (see 'get the value in another record' )
    http://allenbrowne.com/subquery-01.html


    Thank you. Example looks similar in some way.

    But can someone write me exactly how to make a query for my example?

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,858
    Quote Originally Posted by Ivan_CRO View Post
    Thank you. Example looks similar in some way.

    But can someone write me exactly how to make a query for my example?
    I would not say similar, but exactly?, and also using meter readins.

    What is it that you do not understand about it?
    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

  5. #5
    Ivan_CRO is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2021
    Posts
    10
    Quote Originally Posted by Welshgasman View Post
    I would not say similar, but exactly?, and also using meter readins.

    What is it that you do not understand about it?
    Unfortunately, I was on the road yesterday and today and I don't have access to Access so i can't try this example and modify my example to see if it works. So I just read answer on my cell phone, and it seemed different to me.

    But OK, I'll try it tomorrow and see if that's it. If I get stuck somewhere I will ask a specific question where I have a problem.

  6. #6
    Ivan_CRO is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2021
    Posts
    10
    I managed to get to Access and modify the subquery from the @Micron example for my table, and it looks like this ...

    SELECT TEST.DATUM, TEST.OMM, TEST.OCITANJE, (SELECT TOP 1 Dupe.OCITANJE
    FROM TEST AS Dupe
    WHERE Dupe.OMM = TEST.OMM
    AND Dupe.DATUM < TEST.DATUM
    ORDER BY Dupe.DATUM DESC, Dupe.OMM) AS PriorValue
    FROM TEST;

    And resultat that I get is this:


    Click image for larger version. 

Name:	slika_2021-11-27_132352.png 
Views:	8 
Size:	24.2 KB 
ID:	46767

    So I get the value from the previous day. But I still don’t get the “subtraction operation” in that new column. My specific question is where do i insert the subtract operation in the subquery to get the values as from my introductory post?

  7. #7
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,397
    try

    (SELECT TOP 1 TEST.OCITANJE-Dupe.OCITANJE......

    or in the main query

    TEST.OCITANJE-(SELECT TOP 1 Dupe.OCITANJE......

  8. #8
    Ivan_CRO is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2021
    Posts
    10
    Quote Originally Posted by Ajax View Post
    try

    (SELECT TOP 1 TEST.OCITANJE-Dupe.OCITANJE......
    Work this one! Thank You

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

Similar Threads

  1. Subtracting One Sum Total From Another
    By ribena1980 in forum Queries
    Replies: 5
    Last Post: 09-28-2015, 01:01 AM
  2. Replies: 3
    Last Post: 04-02-2015, 06:21 AM
  3. vba > subtracting time
    By metokushika in forum Access
    Replies: 2
    Last Post: 12-09-2014, 09:17 PM
  4. Replies: 3
    Last Post: 03-26-2014, 10:49 AM
  5. Adding/Subtracting
    By JayX in forum Access
    Replies: 1
    Last Post: 12-15-2011, 01:47 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