SQL Server求和查询
今天我的问题是我有 2 列,我希望检查这些列的总和是否不大于某个值(例如 485),如果是则执行查询...
我想做
SELECT * FROM table WHERE ColumnA + ColumnB < 485
但事实并非如此正在工作...我已经尝试过,
SELECT Sum(ColumnA) + Sum(ColumnB) AS Total FROM table
但它给了我 1 列,其中包含所有行的总和,而我想要每个总和都有一行。那么我该怎么办..? xD 我希望你理解,如果不是只是要求我尝试更好地解释它!并提前感谢那些帮助我的人!
编辑:我发现 XD 问题是列是 Smallint 并且 1 行或多行的结果超过 32k,所以它不起作用!谢谢大家!!
Today my problem is this I have 2 columns and I wish to check if the sum of those columns isn't bigger than a value (485 for example) and if is do a query...
I though to do
SELECT * FROM table WHERE ColumnA + ColumnB < 485
But it isn't working... I've already tried with
SELECT Sum(ColumnA) + Sum(ColumnB) AS Total FROM table
but it gives me 1 column with the sum of all rows, I instead want a row for every sum. So how can I do..? xD I hope you understood if not just ask that I try to explain it better! and thanks in advance for those who will help me!
EDIT: I Found out XD the problem was that the columns was Smallint and the result of 1 or more rows was more than 32k so it wasn't working! Thanks all!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实现它的正确查询是:
这样
Select
将返回 ColumnA + ColumnB 小于 485 的所有行如果您想返回两列的总和,您可以使用
这种方式查询对所有行执行列 A 的总和,对所有行执行列 B 的总和,然后将两个总和相加...
The right query to achieve it is:
This way the
Select
will return all the rows where ColumnA + ColumnB is less than 485If otherwise you want in return the sum of the two columns you can use
This way the query do a sum of columnA on all the rows, the sum of ColumnB on all the rows and then sum the two sums...
如果两列中的任何一列中有空值,则必须使用
case when
:也许您有一些空值,或者所有数据的总和值大于这些列的 485,如 David Parvin 所说。
If you have null values in any of the two columns you have to use
case when
:Maybe you have some null values or all of your data have a greater sum value then 485 for these columns as David Parvin stated.