【发布时间】:2011-08-30 19:12:54
【问题描述】:
我有一个产品表,我需要知道表中的记录数。目标是将返回查询分为 2 个单独的查询。
If (Count(*) % 2) = 0 return top(Count(*) / 2)
else return top((Count(*) / 2) + 1)
主要查询是:
select coalesce(Price, ProductPrice) as Price, Product.ProductName, Customer.CustomerName, Product.CatalogNum from Product
inner join Customer on CustomerID = @custId
left outer join CustomerPrice on dbo.Customer.CustomerID = dbo.CustomerPrice.CustomerID
and dbo.Product.ProductID = dbo.CustomerPrice.ProductID
Where Product.ProductActive = 1 Order by Product.CatalogNum
【问题讨论】:
-
如果只需要表中的记录数,可以
SELECT COUNT(*) FROM productTable。但我不认为我理解你的问题...... -
因此,如果行数为偶数,则需要表的上半部分,如果行数为奇数,则上半部分(向下舍入)加一行;是对的吗?哪个版本的 SQL Server?
-
Daniel Renshaw - 正是我的需要。 SQL Server Express 2008 r2
-
从
中选择 count(*) ? -
所有反对票是怎么回事?一个好的问题。
标签: sql sql-server tsql sql-server-2008-r2 sql-server-2008r2-express