sql - Counting how many times each value occurs in a PostgreSQL table? -
Then there is a table with three important columns: store location, customer, and number of purchases. Something like this:
Store | Customer | The number of purchases is a Larry 2B mo 4c curly 7b tina 1 a dina 6c archer 12d malory 3
What I want to do is counting each of the purchases . That is, customers calculate the number of 1 purchase, 2 purchases, 3 purchases, 4 purchases etc. Like histogram, grouped by store.
Store | Buy 1 Buy 2 3 purchases ... a 1 3 2 b 2 1 4 c 1 6 to 8 d4 4 2
There is no manual to do this without knowing the maximum number of purchases There is no clever way too and is there to build one of the branches to count each one of them? Then I already have
select store, calculate as 1_purchase (case number_of_purchases when 1 then 1 and null end) calculation as 2_purchase (case number_of_purchases when 2 then 1 And null end), calculate (case number_of_purchases when 3 then 1 and zero end) as 3_purchase ... by the table group;
But, since the maximum number can change over time, let me calculate this query automatically and keep that account in mind. Any help would be appreciated!
For the format, you will need to use any one of the crosstab () functions in the TableFunk extension. Something along these lines Select
and group from a composite function, number_of_purchases by the selection store, number_of_purchases, from the Table1 group (number_of_purchases) from the store, order shop, number_of_purchases;
Comments
Post a Comment