我有一个SQL查询,显示我的客户和产品的每次购买如何计算两个类别的不同值?
client | product
-----------------------
Lucy Lu | Banana
Lucy Lu | Banana
Lucy Lu | Pineapple
Mad Damon | Banana
Mad Damon | Apple
Mad Damon | Apple
Peter Fox | Banana
Peter Fox | Banana
Peter Fox | Banana
Peter Fox | Apple
Peter Fox | Apple
我想不同的这些查询,但每个产品每个客户的数量,所以我可以为每一个客户看到多少每一个产品他买:
client | product | count
----------------------------
Lucy Lu | Banana | 2
Lucy Lu | Pineapple | 1
Mad Damon | Banana | 1
Mad Damon | Apple | 2
Peter Fox | Banana | 3
Peter Fox | Apple | 2
我count(DISTINCT product)
,count(DISTINCT client)
,count(*)
和GROUP BY (client)
或GROUP BY (product)
尝试过,但没有得到一个有用的解决方案。 当我用SELECT DISTINCT
[查询的其余部分]尝试时,我得到我想要的但没有计数列。
'选择客户,产品,从T组COUNT(*)的客户端,[DBMS](https://en.wikipedia.org/wiki/DBMS)您使用product' –
? Postgres的?甲骨文? –