Create a new column based on calculation from another column of same table

Hi, I want to create a new column (let is called as X) from a column (name Y). X is in Int and Y is in float. The formula is if Y >= 84 then x=Y-84, or if Y<84, then X = Y.

Thank You.

case 
     when Y >=84 then Y-84 
     else  Y end 
as  X
2 Likes

Hi Harish, I tried this and it executed. But once again when i tried to find sum of x, it showing sum of Y. which is wrong.

SUM (
    case  when Y >=84 then Y-84  else  Y end 
    ) 
AS SUM_X

In SQL Server, you can genuniely create another column in your query using a CROSS APPLY:


SELECT SUM(X) AS sum_x
FROM table_name tn
CROSS APPLY (
    SELECT CASE WHEN Y >= 84 THEN Y - 84 ELSE Y END AS X
) AS ca1