如何获取前 50 % 选择查询返回的记录计数。
select top 50 percent *
from Customers
我尝试使用以下查询
select count(*)/2
from Customers
但它不准确,因为当表中的记录总数为 91 时,它将返回 45 条记录,而 TOP 50% 查询返回 46。
我需要一个可以告诉
select TOP 50 percent * from Customers
返回的记录数的查询
最佳答案
你可以用子查询包装它:
SELECT COUNT(*) FROM (select TOP 50 percent * from Customers) s
或使用窗口计数:
select TOP 50 percent *, COUNT(*) OVER() AS cnt from Customers
关于sql-server - SQL Server : how to count records returned from Top 50 percent query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51917325/