How can I get the minimum value after adding value of 2 fields without repeating the where clause?
Example: Get records with minimum value of num1 + num2 and deleted_at is null
id num1 num2 deleted_at
---------- --------- --------- ---------
1 1 1 2015-01-01
2 2 2 null
3 2 2 null
4 4 4 null
Expected result is
id num1 num2 deleted_at
---------- --------- --------- ---------
2 2 2 null
3 2 2 null
I tried subquery, but I have to repeat the where clause
SELECT *
FROM foos t1
WHERE t1.num1 + t1.num2 = (SELECT Min(t2.num1 + t2.num2)
FROM foos t2
WHERE t2.deleted_at IS NULL) //repeat
AND t1.deleted_at IS NULL //repeat
postgresql
,oracle
,db2
,sql-server
, ...