我有一个类似
的Postgresql表user_name | product_name
----------+-------------
tom | candle
bill | candle
bill | candle
tom | pen
bill | pen
我想知道蜡烛销售的分布,即有多少人买了一根蜡烛,有多少人买了两根蜡烛等等。
我试过使用Postgreql窗口函数,但我的头疼了:) http://www.postgresql.org/docs/9.3/static/tutorial-window.html
弗鲁瓦
答案 0 :(得分:3)
select quantity, count(*)
from (
select user_name, count(*) as quantity
from t
where product_name = 'candle'
group by user_name
) s
group by quantity
order by quantity