0

这是SQL,但它给了我一个错误:

select u.productId, count(ProductID) as total, p.Name 
from dbo.UserXProduct AS u 
INNER JOIN dbo.product AS p ON u.ProductID = p.ID   
group by productid, p.name 
having count(ProductID) > 5 
order by u.dateEntered desc, total desc

编辑:这是我得到的错误:

列“dbo.UserXProduct.DateEntered”在 ORDER BY 子句中无效,因为它不包含在聚合函数或 GROUP BY 子句中

4

2 回答 2

1

您不能u.dateEntered直接订购。但是,您可以按max(u.dateEntered). 这可能是您想要的最新产品:

select u.productId, count(ProductID) as total, p.Name 
from dbo.UserXProduct AS u 
INNER JOIN dbo.product AS p ON u.ProductID = p.ID   
group by productid, p.name 
having count(ProductID) > 5 
order by max(u.dateEntered) desc, total desc
于 2013-08-11T13:47:41.420 回答
0

您不能在group by. 试试这个:

select u.productId, count(ProductID) as total, p.Name 
from dbo.UserXProduct AS u 
INNER JOIN dbo.product AS p ON u.ProductID = p.ID   
group by productid, p.name 
having count(ProductID) > 5 
order by count(ProductID) desc

您不能ORDER BY使用 u.dateEntered 列,除非它存在于group by.

于 2013-08-11T03:34:41.023 回答