Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9254163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:23:51+00:00 2026-06-18T11:23:51+00:00

This may be a pipe dream, but I am trying to get these 2

  • 0

This may be a pipe dream, but I am trying to get these 2 commands that work separately, to work as one single command. Instead of XXXXX, I would like to dump in the select statement (which gives me the difference of yesterday’s total and today’s total). The combined command would find the most recent row (max idtanklevel) and update it with the difference of itself and the prior row.

update fw_db.tanklevel2 T3
inner join (select max(idtanklevel) as idtanklevel from fw_db.tanklevel2) T4
on T3.idtanklevel = T4.idtanklevel
set glycolsmallchange = XXXXX;

and

select round(sum((T1.glycolsmall) - (T2.glycolsmall)),2) 
from fw_db.tanklevel2 T1, fw_db.tanklevel2 T2 
where 
T1.idtanklevel = (select max(idtanklevel) from fw_db.tanklevel2)
and 
T2.idtanklevel=(select max(idtanklevel)-1 from fw_db.tanklevel2);

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:23:53+00:00Added an answer on June 18, 2026 at 11:23 am

    Here’s how I would get all that done in a single statement. (Note that this query assumes that idtanklevel is the PRIMARY KEY or at least a UNIQUE KEY in the tanklevel2 table; the query will need to be adjusted if that’s not the case.)

    Some brief comments to document the purpose of each table reference and each inline view:

    -- m  = get id of latest row
    -- t  = get latest row (the row to be updated)
    -- pr = get id for the prior row
    --      pri = source for prior row id
    --      l = id of latest row (identical to "m")
    -- pv = get value from prior row
    -- p  = wrapper so we can join to row returned from pv
    
    UPDATE fw_db.tanklevel2 t
      JOIN ( SELECT MAX(idtanklevel) AS idtanklevel FROM fw_db.tanklevel2 ) m
        ON m.idtanklevel = t.idtanklevel
     CROSS
      JOIN ( SELECT pv.glycolsmall
                  , pv.idtanklevel
               FROM ( SELECT MAX(pri.idtanklevel) AS idtanklevel
                        FROM fw_db.tanklevel2 pri
                       WHERE pri.idtanklevel <
                             (SELECT MAX(l.idtanklevel) FROM fw_db.tanklevel2 l)
                    ) pr
               JOIN fw_db.tanklevel2 pv
                 ON pv.idtanklevel = pr.idtanklevel
              LIMIT 1
           ) p
       SET t.glycolsmallchange = ROUND(t.glycolsmall - p.glycolsmall,2)
    

    Note that the query above uses a slightly different technique to identify the “prior” row. Rather than subtracting 1 from the maximum id value, the query gets the second highest id value (the highest id value that is lower than the maximum value).

    To implement the same strategy your queries are using (i.e. subtracting 1 from the maximum id value, remove this portion of that query above:

               FROM ( SELECT MAX(pri.idtanklevel) AS idtanklevel
                        FROM fw_db.tanklevel2 pri
                       WHERE pri.idtanklevel <
                             (SELECT MAX(l.idtanklevel) FROM fw_db.tanklevel2 l)
                    ) pr
    

    and replace it with this:

               FROM ( SELECT MAX(l.idtanklevel)-1 AS idtanklevel
                        FROM fw_db.tanklevel2 l
                    ) pr
    

    To test this statement, without actually performing an update, remove the SET clause and replace the UPDATE keyword with SELECT and a list of relevant expressions that let you see the rows that are returned e.g.:

    SELECT t.glycolsmallchange                    AS old_glycolsmallchange
         , ROUND(t.glycolsmall - p.glycolsmall,2) AS new_glycolsmallchange
         , t.idtanklevel  AS latest_row_id
         , t.glycolsmall  AS latest_glycolsmall
         , p.idtanklevel  AS prior_row_id
         , p.glycolsmall  AS prior_glycolsmall
      FROM 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may be a pipe dream, but I'm hoping someone knows of a tool
This may be a stupid question, but I'm trying to replicate a customer's OpenLDAP
I'm trying to install MySQLdb extension, but I get this error any idea what
I have a question, which may be a pipe-dream, but I wanted to know
I have already resolved this one but it may help someone out there who
This may sound like a pipe dream, I'm wondering if it's possible. I want
This may be a stupid question but I have a code with the following
This may be have a better name than custom tab completion, but here's the
This may be the wrong place but this is new to me. I did
This may be a simple question but I can;t find the answer anywhere. Here

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.