Results 1 to 5 of 5
  1. #1
    OHB is offline Novice
    Windows 10 Access 2016
    Join Date
    Oct 2021
    Posts
    2

    Retrieve previous row Record to subtract from current row above (water consumption)


    Hello all Members of the Forum,
    I built a query where I want to calculate the difference between two rows in the same column.
    The previous Record is deducted from the next record just the row above and the difference between both value appear in the result of another column (same row).
    My Access SQL formula is as follow :

    SELECT t_compteur.Consomateur, t_compteur.Description, t_releve.Date_releve, t_releve.Releve_m3,
    (SELECT MAX(Releve_m3) from t_releve AS Alias WHERE Alias.Releve_m3 < t_releve.Releve_m3 GROUP BY t_compteur.Description) AS PrevReleve_m3, t_releve.Releve_m3-PrevReleve_m3 AS Difference
    FROM t_compteur RIGHT JOIN t_releve ON t_compteur.N_compteur = t_releve.N_compteur
    ORDER BY t_compteur.Consomateur DESC , t_compteur.Description DESC , t_releve.Date_releve DESC;
    Click image for larger version. 

Name:	Capture.JPG 
Views:	27 
Size:	55.9 KB 
ID:	46389Click image for larger version. 

Name:	Capture2.JPG 
Views:	25 
Size:	107.4 KB 
ID:	46390
    The formula works well with some data but not at all with some other numbers…
    What is wrong with the selection of data for picking wrong number?
    I couldn’t identify the interference and I am wondering if I use the correct formula with the Max() function ?
    Would there be another way to pass-by this bug or another function/formula to be used ?

    Thanks for your support and I appreciated all your useful advices in this Forum.

    Kind regards,
    O.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,822
    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
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    Subqueries are not my favourite thing but I'm going to guess it's because there's only one criteria in your WHERE clause. What should be preventing the random selection of a pair of numbers, a meter ID? It seems that your only criteria is that one number be less than another.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,861
    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
    OHB is offline Novice
    Windows 10 Access 2016
    Join Date
    Oct 2021
    Posts
    2

    Thumbs up SOLVED : Retrieve previous row Record to subtract from current row above

    Quote Originally Posted by June7 View Post
    Big Thanks to June7 with the solution in the document (http://allenbrowne.com/subquery-01.html#AnotherRecord).
    Was missing the "Alias.ID" at the end

    ORDER BY Dupe.ReadDate DESC, Dupe.ID)

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

Similar Threads

  1. Replies: 17
    Last Post: 10-17-2016, 09:21 AM
  2. Replies: 7
    Last Post: 10-06-2014, 01:28 PM
  3. Replies: 2
    Last Post: 03-05-2014, 05:37 PM
  4. Subtract from Previous Row
    By lambo102 in forum Queries
    Replies: 1
    Last Post: 08-06-2011, 09:39 AM
  5. Subtract From Previous Record Using a Date
    By txrules in forum Queries
    Replies: 1
    Last Post: 12-30-2010, 02:10 AM

Tags for this Thread

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