The problem is as follows:
I have this table NUMBERS where I have two columns of numbers:
+----------+----------+
| Number 1 | Number 2 |
+----------+----------+
| 12 | 3 |
| 20 | 7 |
| 17 | 5 |
+----------+----------+
Based on this table I execute an SQL SELECT statement where I want to display the sum of both numbers and whether the sum is bigger than 20 (the Interim_Result as well as the Result must be displayed)
+----------+----------+----------------+----------+
| Number_1 | Number_2 | Interim_Result | Result |
+----------+----------+----------------+----------+
| 12 | 3 | 15 | under 20 |
| 20 | 7 | 27 | over 20 |
| 17 | 5 | 22 | over 20 |
+----------+----------+----------------+----------+
I would write a SQL statement like this:
SELECT
Number_1,
Number_2,
Number_1 + Number_2 AS Interim_Result,
iif(Number_1 + Number_2 > 20, over 20, under 20) AS Result
FROM
NUMBERS
This statement works, but I have duplicate code Number_1 + Number_2
is there a way I can avoid the duplicate code in the iif
and just write:
SELECT
Number_1,
Number_2,
Number_1 + Number_2 AS Interim_Result,
iif(Interim_Result > 20, over 20, under 20) AS Result
FROM
NUMBERS
FYI I am using SQL Server