SQL sql查询添加列值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2220453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sql query adding column values
提问by tibin mathew
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:
考虑上表,我想要一个 SQL 查询,它给我如下结果:
int_id sum(int_test_one,int_test_two)
2 57
3 40
1 38
Is there any sql query to do this?
是否有任何 sql 查询来执行此操作?
回答by Paul Creasey
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
回答by ewernli
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.
如果需要,您可以省略“as”关键字。
回答by Gavin
Try this
尝试这个
SELECT
int_id,
(int_test_one + int_test_two) AS [Total]
FROM
mytable
ORDER BY
[Total] DESC