我有下表和其中的一些示例记录:
id | attr1_id | attr2_id | user_id | rating_id |
------+----------+----------+-------------------+-----------+
1 | 188 | 201 | <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="70050315022f4130141f1d11191e5e131f1d" rel="noreferrer noopener nofollow">[email protected]</a> | 3 |
2 | 193 | 201 | <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bcc9cfd9cee38efcd8d3d1ddd5d292dfd3d1" rel="noreferrer noopener nofollow">[email protected]</a> | 2 |
3 | 193 | 201 | <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="84f1f7e1f6dbb6c4e0ebe9e5edeaaae7ebe9" rel="noreferrer noopener nofollow">[email protected]</a> | 1 |
4 | 194 | 201 | <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0d787e687f523f4d6962606c6463236e6260" rel="noreferrer noopener nofollow">[email protected]</a> | 1 |
5 | 194 | 201 | <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ee9b9d8b9cb1dfae8a81838f8780c08d8183" rel="noreferrer noopener nofollow">[email protected]</a> | 1 |
6 | 192 | 201 | <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fe8b8d9b8ca1ccbe9a91939f9790d09d9193" rel="noreferrer noopener nofollow">[email protected]</a> | 1 |
(attr1_id
、attr2_id
、user_id
) 的组合是UNIQUE
,这意味着每个用户只能创建具有一对特定属性 ID 的一条记录。
我的目标是选择 (attr1_id, attr2_id) 的所有不同组合,其中 rating_id = 1
,但仅选择 attr1_id
和 attr2_id
的每个组合code> 仅一次,且仅在不存在任何其他行(由其他用户)具有 rating_id > 1
且引用相同 attr1_id
和 的情况下attr2_id
。
请注意,attr1_id
和 attr2_id
的组合可以互换,因此给出这两条记录:
id | attr1_id | attr2_id | user_id | rating_id | override_comment
------+----------+----------+--------------------+-----------+------------------
20 | 5 | 2 | <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="44313721361b7504202b29252d2a6a272b29" rel="noreferrer noopener nofollow">[email protected]</a> | 3 |
------+----------+----------+--------------------+-----------+------------------
21 | 2 | 5 | <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="14616771664b2654707b79757d7a3a777b79" rel="noreferrer noopener nofollow">[email protected]</a> | 1 |
不应计算任何行,因为这些行引用 attr_ids
的相同组合,并且其中之一的 rating_id > 1
。
但是,如果这两行存在:
id | attr1_id | attr2_id | user_id | rating_id | override_comment
------+----------+----------+--------------------+-----------+------------------
20 | 5 | 2 | <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="52272137200d6312363d3f333b3c7c313d3f" rel="noreferrer noopener nofollow">[email protected]</a> | 1 |
------+----------+----------+--------------------+-----------+------------------
21 | 2 | 5 | <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d0a5a3b5a28fe290b4bfbdb1b9befeb3bfbd" rel="noreferrer noopener nofollow">[email protected]</a> | 1 |
------+----------+----------+--------------------+-----------+------------------
22 | 2 | 5 | <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="295c5a4c5b761a694d4644484047074a4644" rel="noreferrer noopener nofollow">[email protected]</a> | 1 |
所有行只能算作一行,因为它们都共享相同的 attr1_id
和 attr2_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 个引用同一组 (attr1_id
,attr2_id
)。
因此,我要查找的数字为 (77-12) = 65
。然而,这里的结果似乎是77-2 = 75
。因此,只有存在两个具有相同属性 ID 的评级的行才会被丢弃。
我还要指出my previous question for this matter我被要求开一个新的。
最佳答案
我认为这符合您的描述:
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) ;
我不理解您查询中的其他表,因为您从一个包含您需要的所有内容的表开始。
关于sql - PostgreSQL - 选择条件满足的不同(列1,列2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26908868/