This question already has an answer here:
这个问题在这里已有答案:
- SQL to find the number of distinct values in a column 8 answers
- SQL在列8答案中查找不同值的数量
In PostgreSQL 9.4 I have a table:
在PostgreSQL 9.4中我有一个表:
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.
有没有办法只用一个查询计算表中所有不同的p_id值(不使用子查询)。我可以*使用任何窗口功能。
1 个解决方案
#1
4
You can use the distinct
modifier in the count
function:
您可以在count函数中使用distinct修饰符:
SELECT COUNT(DISTINCT p_id) FROM mytable
#1
4
You can use the distinct
modifier in the count
function:
您可以在count函数中使用distinct修饰符:
SELECT COUNT(DISTINCT p_id) FROM mytable