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!

Need 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;

For the format, you will need to use any one of the crosstab () functions in the TableFunk extension. Something along these lines Select 'num_of_purchases' from the' select shop, number_of_purchases, store (number_of_purchases from Table1 group), order1, 2 to number_of_purchases', 'generate_series (select n from crosstab (select from) 1, 12) as the sequence 1 of n '(store text, "1" int, "2" int, "3" int, "4" int, "5" int, "6" int, "7" Int, "8" int, "9" int, "10" int, "11" int, "12" int); Personally, I do not like the crosstace for such data. You can end up with the output, which are hundreds or thousands of columns wide, most of which are "cell" Are empty


Comments

Popular posts from this blog

winforms - C# Form - Property Change -

javascript - amcharts makechart not working -

java - Algorithm negotiation fail SSH in Jenkins -