codelone
codelone

Reputation: 752

Subtract row values of 2 columns within same table in SQL query

I'm trying to subtract the row values of 2 different columns in the same table.

Table schema

--------------------------------------------
id | com  | type | refund | support | status
--------------------------------------------
1  | 1.17 | res  | 0.00   | NULL    | 0
2  | 3.00 | sys  | 1.85   | 12      | 1
3  | 4.00 | sys  | 0.00   | NULl    | 0
4  | 8.00 | par  | 2.00   | 28      | 1

I want to subtract com - refund if support column is not NULL and status = 1

Output should be like as

------------
id | output 
------------
2  | 1.15 |
4  | 6.00 |

Upvotes: 0

Views: 3327

Answers (1)

user17838564
user17838564

Reputation: 48

Try this:

SELECT ID, (COM - REFUND) AS OUTPUT
FROM TABLE_NAME
WHERE SUPPORT IS NOT NULL AND STATUS = 1; 

The 'as output' names the column in your result set. The where statement filters out your rows by the conditions you have given. The FROM clause tells on which table to execute the query. You have to put your own table name.

Upvotes: 2

Related Questions