按查询分组和 SQL Server 2008

发布于 2024-12-12 02:07:12 字数 178 浏览 0 评论 0原文

我有这个查询

select SUM(Qty) as Qty
from WorkTbl  group by Status
having Status = 'AA' or Status = 'BB'

这个查询返回 2 行(100 和 500)

如何对这两行求和?

I have this query

select SUM(Qty) as Qty
from WorkTbl  group by Status
having Status = 'AA' or Status = 'BB'

this query returns 2 rows (100 and 500)

How to sum those 2 rows ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

清醇 2024-12-19 02:07:12

取出GROUP BY,并使用WHERE而不是HAVING

select SUM(Qty) as Qty
from WorkTbl
where Status = 'AA' or Status = 'BB'

或者,如果您的查询有更多内容,并且您希望保留大部分当前结构,请将其放入子查询(或 CTE)中:(

select SUM(Qty) from (
select SUM(Qty) as Qty
from WorkTbl  group by Status
having Status = 'AA' or Status = 'BB'
) t

我们必须在末尾包含 t,因为from 子句中的每个行源都必须有一个名称 - 它可以是任何名称)

Take out the GROUP BY, and use WHERE instead of HAVING?

select SUM(Qty) as Qty
from WorkTbl
where Status = 'AA' or Status = 'BB'

Or, if there's more to your query, and you wish to keep most of the current structure, put it into a subquery (or a CTE):

select SUM(Qty) from (
select SUM(Qty) as Qty
from WorkTbl  group by Status
having Status = 'AA' or Status = 'BB'
) t

(We have to include the t at the end, since every row source in the from clause has to have a name - it could be anything)

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