PostgreSQL - 选择条件成立的distinct(column1,column2)

时间:2014-11-13 12:26:46

标签: sql postgresql select count distinct

我有下表和一些示例记录:

  id  | attr1_id | attr2_id |      user_id      | rating_id |
------+----------+----------+-------------------+-----------+
 1    |      188 |      201 | user_1@domain.com |         3 |
 2    |      193 |      201 | user_2@domain.com |         2 |
 3    |      193 |      201 | user_2@domain.com |         1 |
 4    |      194 |      201 | user_2@domain.com |         1 |
 5    |      194 |      201 | user_1@domain.com |         1 |
 6    |      192 |      201 | user_2@domain.com |         1 |

attr1_idattr2_iduser_id)的组合为UNIQUE,这意味着每个用户只能创建一条具有特定属性ID的记录。

我的目标是选择rating_id = 1所在的(attr1_id,attr2_id)的所有不同组合,但只选择attr1_idattr2_id的每个组合只有一次,并且只在那里' t存在具有rating_id > 1并引用相同attr1_idattr2_id的任何其他行(由其他用户)。 请注意,attr1_idattr2_id的组合可以切换,因此给出了以下两条记录:

  id  | attr1_id | attr2_id |      user_id       | rating_id | override_comment
------+----------+----------+--------------------+-----------+------------------
  20  |       5  |       2  | user_1@domain.com  |         3 |
------+----------+----------+--------------------+-----------+------------------
  21  |       2  |       5  | user_2@domain.com  |         1 |

不应计算任何行,因为行引用attr_ids的相同组合,其中一行有rating_id > 1

但是,如果存在这两行:

  id  | attr1_id | attr2_id |      user_id       | rating_id | override_comment
------+----------+----------+--------------------+-----------+------------------
  20  |       5  |       2  | user_1@domain.com  |         1 |
------+----------+----------+--------------------+-----------+------------------
  21  |       2  |       5  | user_2@domain.com  |         1 |
------+----------+----------+--------------------+-----------+------------------
  22  |       2  |       5  | user_3@domain.com  |         1 |

所有行都应该只计为一行,因为它们都共享attr1_idattr2_id的相同组合,并且都有rating_id = 1

另外,有一些加入表格列的加入和过滤我会遗漏,但我想我还是会提到它。

SQL Fiddle现在不适合我,但我uploaded some sample data from the compatibility table.

到目前为止,我的查询是:

SELECT distinct(a1, a2),
       a1,
       a2
FROM
  ( SELECT c.*,
           least(attr1_id, attr2_id) AS a1,
           greatest(attr1_id, attr2_id) AS a2
   FROM compatibility c
   JOIN attribute a ON c.attr1_id = a.id
   JOIN PARAMETER pa ON a.parameter_id = pa.id
   JOIN problem p ON pa.problem_id = p.id
   WHERE p.id = 1
   GROUP BY 1,
            2 HAVING NOT bool_or(rating_id > 1)) s;

在样本中,总共有144个评级。每个用户创建了7个评分,其中rating_id > 1和这14个评分中有2个,其中2个是同一组(attr1_idattr2_id)。 因此,我正在寻找的数字是(77-12) = 65。但是,这里的结果似乎是77-2 = 75。因此,只有具有相同属性ID的两个评级存在的行才会被丢弃。

我还要指出my previous question for this matter我被要求开一个新的。

1 个答案:

答案 0 :(得分:2)

我认为这符合你的描述:

select least(attr1_id, attr2_id) as attr1, greatest(attr1_id, attr2_id) as attr2
from table t
group by least(attr1_id, attr2_id), greatest(attr1_id, attr2_id) 
having bool_and(rating_d = 1) ;

我不了解您查询中的其他表格,因为您从一个包含所需内容的表开始。

相关问题