Is there a way to count a number of distinct values?

398 views Asked by At

In PostgreSQL 9.4 I have a table:

id         p_id
PK        integer
-----------------
1           1
2           1
.............
123122       2233

Is there a way to count all distinct p_id values in the table with only one query (without using subqueries). I'm free to use any window-function.

1

There are 1 answers

4
Mureinik On BEST ANSWER

You can use the distinct modifier in the count function:

SELECT COUNT(DISTINCT p_id) FROM mytable