Add column based on condition in SQL

446 Views Asked by At

I have 3 columns with values ranging from 0.1 to 0.9. I want to create a calculated field in sql which would give an output 'Y' if any of the 3 columns has a value greater than 0.8, if not the output should be 'N'. Please see below example - Any help would be appreciated, thanks !

Table example

enter image description here

1

There are 1 best solutions below

0
Jaytiger On

Many DBMS provides GREATEST() function to find a max value from given columns, so using it,

SELECT *, IF(GREATEST(col1, col2, col3) > 0.8, 'Y', 'N') AS calculated_col 
  FROM (
    SELECT 0.8 col1, 0.2 col2, 0.3 col3 UNION ALL
    SELECT 0.3, 0.3, 0.2 UNION ALL
    SELECT 0.2, 0.9, 0.5
  );

Note : you're mentioning only greater than 0.8 except equal to 0.8, so the below result looks different from yours.

enter image description here