Ivan Kuckir
Ivan Kuckir

Reputation: 2559

SQL SELECT : map columns to new column

I have a table with two columns of integers. I would like to select data from it (in some standard way) and add a new column by rule:

Example:

16 | 15 | 1
20 | 28 |-1
11 | 11 | 0
28 | 14 | 1
...

Upvotes: 1

Views: 2137

Answers (2)

GarethD
GarethD

Reputation: 69809

Sounds like you want the SIGN Function

SELECT Col1, Col2, SIGN(Col1 - Col2) AS Col3
FROM   T

Upvotes: 5

Chad
Chad

Reputation: 7517

SELECT X,Y,
    CASE WHEN X > Y THEN 1
         WHEN X < Y THEN -1
         ELSE 0 END AS "Z"
FROM table_name

Upvotes: 5

Related Questions