我有一个产品表,我需要知道的表中的记录数。目标是将返回查询划分为2个单独的查询。SQL计数查询。偶数奇数
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'。但我不认为我理解你的问题...... – jadarnel27
所以,你要表的上半部分,如果行数是偶数,上半部分(舍去),加上一行,如果行数是奇数;是对的吗?哪个版本的SQL服务器? –
Daniel Renshaw - 正是我的需要。 SQL Server Express 2008 r2 – Kulpemovitz