简体   繁体   English

使用具有反转值的列消除行

[英]Eliminate rows with columns with inverted values

I am having so much trouble trying to work this out. 我在努力解决这个问题时遇到了很多麻烦。 Can somebody help me? 有人能帮助我吗?

I have a table with colA and colB and these as the values for each row: 我有一个包含colA和colB的表,这些是每行的值:

colA  ColB
2     1
1     2

When selecting in this table, I want to retrieve just one row since the values are inverted. 在此表中选择时,我想只检索一行,因为值被反转。 If 2 implies 1, 1 implies 2, so they are equal and I don't want two rows as a return. 如果2意味着1,1意味着2,那么它们是相等的,我不希望两行作为回报。

Is there any way of doing a SQL query and get the result I want? 有没有办法进行SQL查询并获得我想要的结果? For example, just one row with colA = 1, colB = 2. 例如,只有一行colA = 1,colB = 2。

If it is only 2 columns: 如果它只有2列:

WITH t(colA, colB) AS (
    VALUES (1,2), (2,1)
)
SELECT DISTINCT least(colA, colB), greatest(colA, colB)
  FROM t;

EDIT: As outlined by Daniel, although this might do the job, this approach is not good for filtering as it'll change the original data. 编辑:正如Daniel所述,虽然这可能会起作用,但这种方法不适合过滤,因为它会改变原始数据。

No indication was made as to which version of the tuple is preferred over the other. 没有迹象表明哪个版本的元组优先于另一个版本。 Ie, we know that (1,2) is equivalent to (2,1) and in that case one of the two should be display, but which of the two? 即,我们知道(1,2)相当于(2,1) ,在这种情况下,两者中的一个应该显示,但两者中的哪一个? In this solution, the assumption is that the variant with the lower ColA value is preferred over the reverse. 在该解决方案中,假设具有较低ColA值的变体优先于反向。

Select ColA, ColB
From SourceData As S
Where ColA < ColB
    Or Not Exists   (
                    Select 1
                    From SourceData As S1
                    Where S1.ColA = S.ColB
                        And S1.ColB = S.ColA
                    )

The SQL SELECT ... EXCEPT SELECT ... construct may be used for this. 可以使用SQL SELECT ... EXCEPT SELECT ...构造。 Also it will probably be faster than queries involving a self-join, especially without any index. 它也可能比涉及自连接的查询更快,特别是没有任何索引。 I would suggest: 我会建议:

SELECT colA, colB FROM table
  EXCEPT
SELECT colB, colA FROM table WHERE colA < colB;

Duplicates rows are eliminated (if not needed or not desirable, use EXCEPT ALL ). 消除了重复行(如果不需要或不需要,请使用EXCEPT ALL )。

Rows where colA=colB are not a special case, they go into the result, de-duplicated. colA = colB的行不是特殊情况,它们会进入结果,重复数据删除。

A self join might work. 自我加入可能会奏效。

select t1.cola, t1.colb
from tablename t1 join tablename t2 on something
where t1.cola <> t2.colb
and t1.colb <> t2.cola

How about using arrays? 如何使用数组?

select a, b from t;

/*
 a | b 
---+---
 1 | 2
 2 | 1
(2 rows)
*/

create extension intarray;
select distinct sort(ARRAY[a, b]) from t;

/*    
 sort  
-------
 {1,2}
(1 row)
*/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM