在聚合表达式中使用子查询

时间:2011-03-21 14:24:48

标签: tsql

而不是:

SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer

如何通过子查询生成COUNT来获得上述内容。

SELECT Customer,Count(select * from Orders where o.idUser = u.idUser) FROM Orders o 
inner join Users u on u.idOrder = o.idOrder
GROUP BY Customer

THX

1 个答案:

答案 0 :(得分:0)

这应该这样做,我认为不需要在Topquery中包含Orders:

SELECT u.Customer, (SELECT COUNT(*) FROM Orders o WHERE o.idUser = u.idUser)
FROM Users u