Mira Ghazi
Mira Ghazi

Reputation: 11

How to subtract the content of a column of two rows

I have a table like this

MY table

and I want to return the difference between the two rows

Upvotes: 0

Views: 47

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269563

SQL tables represent unordered sets. There is no ordering, unless a column specifies the ordering.

So, you can get the two values using MAX() and MIN(). This should do what you want:

select max(nbaction) - min(nbaction)
from t;

EDIT:

Given your actual problem, you have multiple choices. Here is one:

SELECT (SELECT nbaction
        FROM analyse_page_fait 
        WHERE operateurdimid = 2
        ORDER BY datedimid DESC
        FETCH FIRST 1 ROW ONLY
       ) -
       (SELECT nbaction
        FROM analyse_page_fait 
        WHERE operateurdimid = 2
        ORDER BY datedimid DESC
        OFFSET 1
        FETCH FIRST 1 ROW ONLY
       ) as diff

Upvotes: 1

Related Questions