我有一个包含这两个字段的客户表(实际上更多,但这是简化的):customerAcct,product
我需要计算每个产品具有一定数量的客户数量。结果如下:
Accounts Product Quantity
100 ProductA 3
40 ProductA 2
20 ProductA 1
80 ProductB 3
60 ProductB 2
30 ProductB 1
等等。所以基本上它告诉我100个客户有3个ProductA,40个客户有2个ProductA,20个客户有1个ProductA。然后同样适用于ProductB 提前谢谢。
答案 0 :(得分:0)
试试这个:
SELECT COUNT(*) AS Accounts, product, cnt AS Quantity
FROM (
SELECT customerAcct, product, COUNT(*) AS cnt
FROM Customer
GROUP BY customerAcct, product) AS t
GROUP BY product, cnt