Using a subquery in an aggregate expression
Instead of this:
SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer
How do I get something like above but by producing a COUNT fr开发者_运维知识库om a subquery.
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
This should do, I see no need to include Orders in the Topquery:
SELECT u.Customer, (SELECT COUNT(*) FROM Orders o WHERE o.idUser = u.idUser)
FROM Users u
精彩评论