Sql query adding column values

I want to add two column values ​​of my table and sort them in descending order. For instance:

int_id   int_test_one  int_test_2
 1           25           13    
 2           12           45    
 3           25           15

Given the table above, I want the SQL query to give me the result, as shown below:

   int_id  sum(int_test_one,int_test_two)
    2              57
    3              40   
    1              38

Is there any sql query for this?

+3
source share
3 answers

There is no built-in function for such horizontal aggregation, you can just do ...

SELECT INT_ID, INT_TEST_ONE + INT_TEST_TWO AS SUM FROM TABLE
+7
source

Did you try to describe what you described? It works:

SELECT int_id , ( int_test_one + int_test_two ) as s FROM mytable ORDER BY s DESC

You can omit the "how" keyword if you want.

+3
source

SELECT 
    int_id, 
    (int_test_one + int_test_two) AS [Total] 
FROM 
    mytable 
ORDER BY 
    [Total] DESC
+1

Source: https://habr.com/ru/post/1731836/


All Articles