user3498459
user3498459

Reputation:

SQL is giving an error even though seems to be correct

Giving an error, I was trying to see what's wrong, to no avail. Please help

 SELECT UserId, AVG(Total) AS AvgOrderTotal
FROM Invoices
HAVING COUNT(OrderId) >= 1

Upvotes: 0

Views: 723

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269803

You are missing the GROUP BY:

SELECT UserId, AVG(Total) AS AvgOrderTotal
FROM Invoices
GROUP BY UserId
HAVING COUNT(OrderId) >= 1

Assuming that OrderId is never NULL, the HAVING is redundant, so perhaps this is sufficient:

SELECT UserId, AVG(Total) AS AvgOrderTotal
FROM Invoices
GROUP BY UserId;

Upvotes: 1

Related Questions