3
CusID   Order
3001    Hotdog
3001    Sausage
3002    Sausage
3003    Burger
3003    Hotdog
3002    Hotdog
3001    Burger

How I will use this count function?

SELECT COUNT(CusID) AS NumOfOrders FROM Orders

Desired output

CusID   NumOfOrders
3001    3
3002    2
3003    2
0

4 Answers 4

6

You need it with a group by:

SELECT CusId, COUNT(CusID) AS NumOfOrders
FROM Orders
group by CusId
order by CusId;
Sign up to request clarification or add additional context in comments.

Comments

1

The group by will give you that desired result along with Count function.

select CustID, count(CustID) from Orders
group by CustID

Refer below fiddle link for sample

SQL Fiddle

Comments

0
select CusID,count(CusID) from Orders group by CusID order by CusId

Comments

0

Try adding GROUP BY and ORDER BY in your query.

SELECT CusId, COUNT(CusID) AS NumOfOrders
FROM Orders
GROUP BY CusId
ORDER BY CusId;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.