我只想要品牌(蓝色、白色、红色)的计数,如果它们对应的客户价值被多次列出。
*customer* *productid* *brand*
1 A Red
2 B Blue
1 A Red
2 C Blue
3 B White
1 A Red
2 B Blue期望的结果:我想要一个单一的数据集,其中包含品牌及其计数,仅针对重复购买者的客户。
*brands* *repeat_purchase*
Red 3
Blue 2select customer, productid, count(productid) as repeat_purchase
from Public."CustomerData"
group by customer, productid
having count(productid) > 1;上面就是我到目前为止所拥有的,但我想不出如何只有两列:一列是每个品牌的名称,另一列是每个品牌包含在重复购买中的总次数。
发布于 2019-11-15 09:42:10
您的问题似乎需要两个级别的聚合:
select brand, sum(cnt)
from (select customer, product, brand, count(*) as cnt
from Public."CustomerData"
group by customer, product, brand
having count(*) >= 2
) t
group by brand;https://stackoverflow.com/questions/58869124
复制相似问题