3
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
4

4 回答 4

6

你需要它group by

SELECT CusId, COUNT(CusID) AS NumOfOrders
FROM Orders
group by CusId
order by CusId;
于 2013-07-22T01:46:14.047 回答
1

group by将为您提供所需的结果以及Count功能。

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

请参阅下面的小提琴链接以获取示例

SQL小提琴

于 2013-07-22T03:05:03.370 回答
0
select CusID,count(CusID) from Orders group by CusID order by CusId
于 2013-07-22T04:51:19.590 回答
0

尝试在查询中添加GROUP BYORDER BY

SELECT CusId, COUNT(CusID) AS NumOfOrders
FROM Orders
GROUP BY CusId
ORDER BY CusId;
于 2013-07-22T07:34:29.217 回答