执行SQL查询并同时查找返回的记录数

发布于 2024-10-11 14:57:34 字数 244 浏览 5 评论 0原文

我有这样的查询:

SELECT   YEAR,
         period,
         ROUND(a.NUMERATOR/b.total_sum, 0) avg_val FROM

(Select ... ) subQuery1,
(Select ... ) subQuery2

ORDER BY YEAR, period

我还想知道查询返回的记录数。

我应该如何修改查询?

I have query something like this:

SELECT   YEAR,
         period,
         ROUND(a.NUMERATOR/b.total_sum, 0) avg_val FROM

(Select ... ) subQuery1,
(Select ... ) subQuery2

ORDER BY YEAR, period

I also want to know the number of records the query is returning.

How should I modify the query?

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

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

发布评论

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

评论(2

家住魔仙堡 2024-10-18 14:57:34

我知道在 SQL Server 中要获取受 SQL 语句影响的行数,您应该从查询中返回 @@rowcount 。在Oracle中,它应该是类似的东西,比如我猜的sql%rowcount,根据这篇文章:
http://www.dbasupport.com/forums/showthread.php?t= 20077

另请参阅这篇文章在 SELECT 语句后需要行计数:最佳 SQL 方法是什么?

I know that in SQL Server to get the number of rows affected by the SQL statement you should return @@rowcount from your query. In Oracle it should be something similar, like sql%rowcount i guess, according with this post:
http://www.dbasupport.com/forums/showthread.php?t=20077

Also have look this post Need a row count after SELECT statement: what's the optimal SQL approach?

烟─花易冷 2024-10-18 14:57:34

假设查询返回 N 行。您想要将数字 N 添加到每一行吗?

我想不会。然后,您需要一个单独的查询来返回查询结果中的行数。您可以这样做:

SELECT COUNT(*) FROM
(Select ... ) subQuery1,
(Select ... ) subQuery2

顺便说一句,最好在 subQuery1 和 subQuery2 之间使用 JOIN 而不仅仅是逗号。 JOIN 将使您的查询更具可读性。

Let's say the query returns N rows. Do you want the number N to be added to EVERY row?

I suppose not. Then you'll need a separate query for returning the number of rows in the results of your query. You can do it like this:

SELECT COUNT(*) FROM
(Select ... ) subQuery1,
(Select ... ) subQuery2

By the way, it's better to use a JOIN between subQuery1 and subQuery2 and not just a comma. A JOIN will make your query more readable.

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