R1234
R1234

Reputation: 484

Sql Query for finding a certain frequency distribution

This table is updated whenever a customer buys anything:

table 1

I want to find out a query to know the frequency distribution of number of customers and number of items bought. For example the number of customers who have bought 1 item, 2 items and so on.

Upvotes: 1

Views: 4120

Answers (1)

mu is too short
mu is too short

Reputation: 434755

Something like this should do the trick:

select items_bought, count(*) as customers
from (
    select customerid, count(*) as items_bought
    from your_table
    group by customerid
) dt
group by items_bought

First you group by customerid to get your counts and then group by the counts to get your histogram values. This will give you the number of items bought in items_bought and the number of customers that bought that many in customers.

Upvotes: 9

Related Questions