0

I'm new to SQL and I have the following problem.
I want to sum total number of errors that are reaching my device.

So, suppose I have inside my_table variables with names SeqErr and TotalSeqErr.
I initialized them to 0 and verified it, but when I do the following:

  UPDATE my_table
  SET
  ...
    SeqErr         =      COALESCE(SeqErr, 0) + 1,
    TotalSeqErr    =      SeqErr,
  ...

I see that when I retrieve this table it is coming with the following values:
SeqErr = 1 , TotalSeqErr = 0 (i.e. SeqErr was updated but TotalSeqErr is using old SeqErr value instead of updated one).

How can I solve this issue?
Note: Probably there is issue similar to mine here but I couldn't find one.

3
  • I'm using PostgreSQL as my SQL database if this is what you meant. Commented Oct 7, 2020 at 9:37
  • 1
    You will need to run that UPDATE at least twice to see a non-zero value in totalseqerr (but that assignment should also use coalesce()) dbfiddle.uk/… Commented Oct 7, 2020 at 9:43
  • Btw: seqerr is not a "variable", it's called a column Commented Oct 7, 2020 at 9:52

2 Answers 2

1

If you want your TotalSeqErr to use the "new" SeqErr you could just update it the same way you update SeqErr:

...
SeqErr         =      COALESCE(SeqErr, 0) + 1,
TotalSeqErr    =      COALESCE(SeqErr, 0) + 1,
...

You could imagine that update is done all at once not line by line. So what you have in the right side of the equal refers to the old values even though you set them to a new value a line above.

Sign up to request clarification or add additional context in comments.

3 Comments

So there is no way to do so inside one query? (In case I want TotalSeqErr to get SeqErr updated variable I will need it to be inside another query)
@ms_stud: that is one query.
Added an explanation to my answer, let me know if that helps or if you require extra informaton. Note that COALESCE(SeqErr, 0) + 1 is not another query, it is another expression.
1

The way an update works is that the references on the "left" (i.e. the set columns) refer to the new record. The references on the "right" (i.e. the expressions) refer to the old values. This is how SQL is defined. I only know of one database that does not support the standard behavior, MySQL/MariaDB.

One method is to repeat the expression:

UPDATE my_table
  SET . . .
      SeqErr      =  COALESCE(SeqErr, 0) + 1,
      TotalSeqErr =  COALESCE(SeqErr, 0) + 1,
      ...;

Assuming your table has a primary key, you can also simplify the SET logic by using a FROM clause and a lateral join:

UPDATE my_table t
      SET . . .
          SeqErr      =  v.new_SeqErr,
          TotalSeqErr =  v.new_SeqErr,
          ...
    FROM my_table t2 CROSS JOIN LATERAL
         (VALUES (COALESCE(t2.SeqErr, 0) + 1)
         ) v(new_seqerr)
    WHERE t2.<primary key> = t.<primary key>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.