SQL: 检索不同列的 Distinct
SQL: Retrieve Distinct for different columns
我有以下场景:
在 SQL-Table 我有以下列:
|----------------------------------------|-------------------------------------|
| Col 1 | Col 2 |
|----------------------------------------|-------------------------------------|
| 4BAEFCAD-0B61-E911-B26B-005056872FC1 | 855757A6-0D61-E911-B26B-005056872FC1|
|----------------------------------------|-------------------------------------|
| 855757A6-0D61-E911-B26B-005056872FC1 | 4BAEFCAD-0B61-E911-B26B-005056872FC1|
|----------------------------------------|-------------------------------------|
| D2ADDEF8-A3A8-E911-B272-005056872FC1 | CED9DFD0-35A9-E911-B272-005056872FC1|
|----------------------------------------|-------------------------------------|
前两行有点引用相同的记录,因为它们包含第 1 行和第 2 行的相同值但交换了。
是否有一种 sql 方法可以只检索这两个列之一?所以最后我收到以下结果:
|----------------------------------------|-------------------------------------|
| Col 1 | Col 2 |
|----------------------------------------|-------------------------------------|
| 4BAEFCAD-0B61-E911-B26B-005056872FC1 | 855757A6-0D61-E911-B26B-005056872FC1|
|----------------------------------------|-------------------------------------|
| D2ADDEF8-A3A8-E911-B272-005056872FC1 | CED9DFD0-35A9-E911-B272-005056872FC1|
|----------------------------------------|-------------------------------------|
谢谢
一个简单的方法是:
select col1, col2
from t
where col1 < col2
union all
select col1, col2
from t
where col2 > col1 and
not exists (select 1 from t t2 where t2.col2 = t.col1 and t2.col1 = t.col2);
这样做的好处是可以保留原始行。如果您不关心顺序:
select distinct (case when col1 < col2 then col1 else col2 end) as col1,
(case when col1 < col2 then col2 else col1 end) as col2
from t;
我有以下场景:
在 SQL-Table 我有以下列:
|----------------------------------------|-------------------------------------|
| Col 1 | Col 2 |
|----------------------------------------|-------------------------------------|
| 4BAEFCAD-0B61-E911-B26B-005056872FC1 | 855757A6-0D61-E911-B26B-005056872FC1|
|----------------------------------------|-------------------------------------|
| 855757A6-0D61-E911-B26B-005056872FC1 | 4BAEFCAD-0B61-E911-B26B-005056872FC1|
|----------------------------------------|-------------------------------------|
| D2ADDEF8-A3A8-E911-B272-005056872FC1 | CED9DFD0-35A9-E911-B272-005056872FC1|
|----------------------------------------|-------------------------------------|
前两行有点引用相同的记录,因为它们包含第 1 行和第 2 行的相同值但交换了。
是否有一种 sql 方法可以只检索这两个列之一?所以最后我收到以下结果:
|----------------------------------------|-------------------------------------|
| Col 1 | Col 2 |
|----------------------------------------|-------------------------------------|
| 4BAEFCAD-0B61-E911-B26B-005056872FC1 | 855757A6-0D61-E911-B26B-005056872FC1|
|----------------------------------------|-------------------------------------|
| D2ADDEF8-A3A8-E911-B272-005056872FC1 | CED9DFD0-35A9-E911-B272-005056872FC1|
|----------------------------------------|-------------------------------------|
谢谢
一个简单的方法是:
select col1, col2
from t
where col1 < col2
union all
select col1, col2
from t
where col2 > col1 and
not exists (select 1 from t t2 where t2.col2 = t.col1 and t2.col1 = t.col2);
这样做的好处是可以保留原始行。如果您不关心顺序:
select distinct (case when col1 < col2 then col1 else col2 end) as col1,
(case when col1 < col2 then col2 else col1 end) as col2
from t;