tags:

views:

48

answers:

3

Hi friends,

I want to add two columns values of my table and sort it in descending order. E.g:

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

Considering the table above, I want a SQL query which give me the result like below:

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

Is there any sql query to do this?

Thanks

+3  A: 

Did you try what you describe? This works:

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

You can ommit the "as" keyword if you want.

ewernli
Don't forget the DESC order :)
AdaTheDev
Tanks, I've added it
ewernli
A: 

There is not built in function for this kind of horizontal aggregation, you can just do...

SELECT INT_ID, INT_TEST_ONE + INT_TEST_TWO AS SUM FROM TABLE
Paul Creasey
Forgot descending sort
David
+1  A: 

Try this

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