Count number of occurrences in Postgresql

Asked

Viewed 208 times

1

I have a database where the values are organized more or less as follows:

id | produto     | data com registro de venda
----------------------------------------------
 1 | Prod1       | 17/09/2019
 2 | Prod1       | 17/09/2019
 3 | Prod2       | 17/09/2019
 4 | Prod2       | 14/09/2019
 5 | Prod1       | 12/09/2019
 6 | Prod1       | 12/09/2019
 7 | Prod1       | 17/09/2019
 8 | Prod1       | 14/09/2019
 9 | Prod3       | 17/09/2019
10 | Prod3       | 17/09/2019
11 | Prod1       | 11/09/2019
12 | Prod1       | 15/09/2019

Information I would like to get

Prod1 = 5 dias com vendas registradas (ignorando os dias repetidos)
Prod2 = 2 dias com vendas registradas (ignorando os dias repetidos)
Prod3 = 1 dia com vendas registradas (ignorando os dias repetidos)
  • Show what you have tried, if you tried something and gave error count, put in the body of the question

  • I haven’t tried anything yet. I’ve just learned to make simple appointments. Situations that require more complex results, as is the case of the question, I still do not know how to do.

  • Dude. You know how to use group by and functions of count and unique

  • 1

    I think what you want is something like: SELECT product, COUNT(DISTINCT data) FROM sua_tabela GROUP BY product; The Postgresql documentation is very good, see it: https://www.postgresql.org/docs/current/index.html

  • @anonimo, that’s right! I’m just in doubt if he wants with the texts, as in the return example

  • If it wanted with the text it would be: SELECT product, ' = ', COUNT(DISTINCT data), ' days with registered sales (ignoring repeat days)' FROM sua_table GROUP BY product;

  • Yesterday I had managed to settle using the DISTINCT. Thank you.

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.