我的sql数据库中有两个表
product: PID(PK), PName Sales: PID(FK), Quantity, Price
我只想实现一个查询,统计每个PID在sales表中重复了多少次
我试过这段代码:
> select count(PID) from sales where PID = 'P01';
但我必须插入一个特定的 ID 号才能计算它。我想要所有 ID 的问题。 我希望有人可以帮助解决这个问题。 谢谢
最佳答案
您需要按 PID
分组并计算每个组中出现的次数:
select
PID,
count(*) as PIDCount
from
sales
group by
PID;
关于sql - sql db2 统计每个id在表中出现的次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25458219/