我有一个产品表,其中的列prodId(产品ID)和消费者ID,其中prodId是主键,对于每个产品我们都有prodId,现在对于每个产品我们都有一个消费者,因为消费者可以拥有许多产品,我需要根据他们拥有的产品数量找到人数。
答案 0 :(得分:0)
您可以使用两种聚合级别:
select numproducts, count(*) as numpeople
from (select consumerid, count(*) as numproducts
from product
group by consumerid
) c
group by numproducts
order by numproducts;
答案 1 :(得分:-1)
请尝试一下
选择 (从产品P1中选择COUNT(产品ID),其中P1.Consumerid = P.Consumerid)AS numberOfproduct, (从产品P2的SELECT COUNT(Consumerid)个P2.Consumerid = P.Consumerid)AS NoOfConsumer 从产品P 按Consumerid分组