我有一個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的?甲骨文? –