SQL Server求和查询

发布于 2024-12-17 05:09:59 字数 424 浏览 1 评论 0原文

今天我的问题是我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甚是思念 2024-12-24 05:09:59

实现它的正确查询是:

SELECT * FROM table WHERE ColumnA+ColumnB<485

这样 Select 将返回 ColumnA + ColumnB 小于 485 的所有行

如果您想返回两列的总和,您可以使用

SELECT SUM(ColumnA)+SUM(ColumnB) FROM Table

这种方式查询对所有行执行列 A 的总和,对所有行执行列 B 的总和,然后将两个总和相加...

The right query to achieve it is:

SELECT * FROM table WHERE ColumnA+ColumnB<485

This way the Select will return all the rows where ColumnA + ColumnB is less than 485

If otherwise you want in return the sum of the two columns you can use

SELECT SUM(ColumnA)+SUM(ColumnB) FROM Table

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...

颜漓半夏 2024-12-24 05:09:59

如果两列中的任何一列中有空值,则必须使用 case when

SELECT * FROM table_name WHERE (CASE WHEN ColumnA IS NULL 0 ELSE ColumnA END) + (CASE WHEN ColumnB IS NULL 0 ELSE ColumnB END) < 485;

也许您有一些空值,或者所有数据的总和值大于这些列的 485,如 David Parvin 所说。

If you have null values in any of the two columns you have to use case when:

SELECT * FROM table_name WHERE (CASE WHEN ColumnA IS NULL 0 ELSE ColumnA END) + (CASE WHEN ColumnB IS NULL 0 ELSE ColumnB END) < 485;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文