Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查另一列的重复项时,一列上的PostgreSQL计数不同_Postgresql_Count_Distinct_Postgresql 9.1 - Fatal编程技术网

检查另一列的重复项时,一列上的PostgreSQL计数不同

检查另一列的重复项时,一列上的PostgreSQL计数不同,postgresql,count,distinct,postgresql-9.1,Postgresql,Count,Distinct,Postgresql 9.1,我有一个查询,结果是这样一个表: guardian_id | child_id | guardian_name | relation | child_name | ------------|----------|---------------|----------|------------| 1 | 1 | John Doe | father | Doe Son | 2 | 1 | Jane Doe

我有一个查询,结果是这样一个表:

guardian_id | child_id | guardian_name | relation | child_name |
------------|----------|---------------|----------|------------|
    1       |   1      | John Doe      | father   | Doe Son    |
    2       |   1      | Jane Doe      | mother   | Doe Son    |
    3       |   2      | Peter Pan     | father   | Pan Dghter |
    4       |   2      | Pet Pan       | mother   | Pan Dghter |
    1       |   3      | John Doe      | father   | Doe Dghter |
    2       |   3      | Jane Doe      | mother   | Doe Dghter |
因此,从这些结果中,我需要计算家庭数量。也就是说,不同的孩子有相同的监护人。根据以上结果,有3个孩子,但有2个家庭。我怎样才能做到这一点

如果我这样做:

SELECT COUNT(DISTINCT child_id) as families FROM (
  //larger query
)a
我会得到3,这是不正确的。 或者,如何合并检查不同监护人id的WHERE子句?还有其他方法吗


还请注意,在某些情况下,一个孩子可能只有一个监护人。

要获得不同的家庭,您可以尝试以下方法

select distinct array_agg(distinct guardian_id) 
from family
group by child_id;
上述查询将返回唯一族的列表。 如。
现在你可以在上面应用计数了。你真的还在使用Postgres9.1吗?这已经失去了支持[两年多了。你应该尽快计划升级。是的,系统严重依赖postgres 9.1,使用相同RDBMS postgres 9.1的多个应用程序-对于升级,团队中其他更高级的成员有发言权
{1,2}
{3,4}