CusID Order
3001 Hotdog
3001 Sausage
3002 Sausage
3003 Burger
3003 Hotdog
3002 Hotdog
3001 Burger
我将如何使用这个计数功能?
SELECT COUNT(CusID) AS NumOfOrders FROM Orders
期望的输出
CusID NumOfOrders
3001 3
3002 2
3003 2
CusID Order
3001 Hotdog
3001 Sausage
3002 Sausage
3003 Burger
3003 Hotdog
3002 Hotdog
3001 Burger
我将如何使用这个计数功能?
SELECT COUNT(CusID) AS NumOfOrders FROM Orders
期望的输出
CusID NumOfOrders
3001 3
3002 2
3003 2
你需要它group by
:
SELECT CusId, COUNT(CusID) AS NumOfOrders
FROM Orders
group by CusId
order by CusId;
这group by
将为您提供所需的结果以及Count
功能。
select CustID, count(CustID) from Orders
group by CustID
请参阅下面的小提琴链接以获取示例
select CusID,count(CusID) from Orders group by CusID order by CusId