执行SQL查询并同时查找返回的记录数
我有这样的查询:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我知道在 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, likesql%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?
假设查询返回 N 行。您想要将数字 N 添加到每一行吗?
我想不会。然后,您需要一个单独的查询来返回查询结果中的行数。您可以这样做:
顺便说一句,最好在 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:
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.