SQL get only unique combination of two columns
up vote
0
down vote
favorite
I have table with:
A B
1 2
2 1
and i trying using sql command to get only one combination
A B
1 2
how can i do that?
sql
add a comment |
up vote
0
down vote
favorite
I have table with:
A B
1 2
2 1
and i trying using sql command to get only one combination
A B
1 2
how can i do that?
sql
Please tag with the database you are using.
– Gordon Linoff
Nov 9 at 21:33
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have table with:
A B
1 2
2 1
and i trying using sql command to get only one combination
A B
1 2
how can i do that?
sql
I have table with:
A B
1 2
2 1
and i trying using sql command to get only one combination
A B
1 2
how can i do that?
sql
sql
edited Nov 9 at 23:35
Salman A
171k65328414
171k65328414
asked Nov 9 at 21:31
C.Frank
39115
39115
Please tag with the database you are using.
– Gordon Linoff
Nov 9 at 21:33
add a comment |
Please tag with the database you are using.
– Gordon Linoff
Nov 9 at 21:33
Please tag with the database you are using.
– Gordon Linoff
Nov 9 at 21:33
Please tag with the database you are using.
– Gordon Linoff
Nov 9 at 21:33
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
A canonical way in standard SQL is:
select a, b
from t
where a < b
union all
select a, b
from t
where a > b and not exists (select 1 from t t2 where t2.a = t.b and t2.b = t.a);
Note that this assumes no duplicates or equal values. You can easily handle these using select distinct and <= comparisons. In my experience, this problem often arises when there are at most two rows per pair.
This preserves the original values. So, if you start with:
1 2
5 4
You will get that in the result set.
If you don't care about ordering, then many databases support least()/greatest():
select least(a, b) as a, greatest(a, b) as b
from t
group by least(a, b), greatest(a, b);
You can do the same thing with case expressions. Or, more simply as:
select distinct least(a, b) as a, greatest(a, b) as b
from t;
...for the second statement, would aDISTINCTbe more idiomatic?
– Clockwork-Muse
Nov 9 at 21:37
Neat. For first one maybe s/where a < b/where a <= b/ ?
– jgreve
Nov 9 at 21:38
@jgreve . . . It depends. But I did elaborate in the answer about the assumptions in the current version and how to extend it.
– Gordon Linoff
Nov 9 at 21:43
add a comment |
up vote
0
down vote
You just need to skip a row if it is a duplicate. 2, 1 and 1, 2 are duplicates of each other in which case we skip 2, 1 as a duplicate of 1, 2.
DECLARE @t TABLE (a INT, b INT);
INSERT INTO @t VALUES
(1, 1),
(1, 2),
(2, 1),
(3, 1);
SELECT DISTINCT a, b
FROM @t AS t
WHERE NOT EXISTS (
SELECT 1
FROM @t AS dup
WHERE a = t.b AND b = t.a AND a < b
)
Result:
| a | b |
|---|---|
| 1 | 1 |
| 1 | 2 |
| 3 | 1 |
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
A canonical way in standard SQL is:
select a, b
from t
where a < b
union all
select a, b
from t
where a > b and not exists (select 1 from t t2 where t2.a = t.b and t2.b = t.a);
Note that this assumes no duplicates or equal values. You can easily handle these using select distinct and <= comparisons. In my experience, this problem often arises when there are at most two rows per pair.
This preserves the original values. So, if you start with:
1 2
5 4
You will get that in the result set.
If you don't care about ordering, then many databases support least()/greatest():
select least(a, b) as a, greatest(a, b) as b
from t
group by least(a, b), greatest(a, b);
You can do the same thing with case expressions. Or, more simply as:
select distinct least(a, b) as a, greatest(a, b) as b
from t;
...for the second statement, would aDISTINCTbe more idiomatic?
– Clockwork-Muse
Nov 9 at 21:37
Neat. For first one maybe s/where a < b/where a <= b/ ?
– jgreve
Nov 9 at 21:38
@jgreve . . . It depends. But I did elaborate in the answer about the assumptions in the current version and how to extend it.
– Gordon Linoff
Nov 9 at 21:43
add a comment |
up vote
2
down vote
accepted
A canonical way in standard SQL is:
select a, b
from t
where a < b
union all
select a, b
from t
where a > b and not exists (select 1 from t t2 where t2.a = t.b and t2.b = t.a);
Note that this assumes no duplicates or equal values. You can easily handle these using select distinct and <= comparisons. In my experience, this problem often arises when there are at most two rows per pair.
This preserves the original values. So, if you start with:
1 2
5 4
You will get that in the result set.
If you don't care about ordering, then many databases support least()/greatest():
select least(a, b) as a, greatest(a, b) as b
from t
group by least(a, b), greatest(a, b);
You can do the same thing with case expressions. Or, more simply as:
select distinct least(a, b) as a, greatest(a, b) as b
from t;
...for the second statement, would aDISTINCTbe more idiomatic?
– Clockwork-Muse
Nov 9 at 21:37
Neat. For first one maybe s/where a < b/where a <= b/ ?
– jgreve
Nov 9 at 21:38
@jgreve . . . It depends. But I did elaborate in the answer about the assumptions in the current version and how to extend it.
– Gordon Linoff
Nov 9 at 21:43
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
A canonical way in standard SQL is:
select a, b
from t
where a < b
union all
select a, b
from t
where a > b and not exists (select 1 from t t2 where t2.a = t.b and t2.b = t.a);
Note that this assumes no duplicates or equal values. You can easily handle these using select distinct and <= comparisons. In my experience, this problem often arises when there are at most two rows per pair.
This preserves the original values. So, if you start with:
1 2
5 4
You will get that in the result set.
If you don't care about ordering, then many databases support least()/greatest():
select least(a, b) as a, greatest(a, b) as b
from t
group by least(a, b), greatest(a, b);
You can do the same thing with case expressions. Or, more simply as:
select distinct least(a, b) as a, greatest(a, b) as b
from t;
A canonical way in standard SQL is:
select a, b
from t
where a < b
union all
select a, b
from t
where a > b and not exists (select 1 from t t2 where t2.a = t.b and t2.b = t.a);
Note that this assumes no duplicates or equal values. You can easily handle these using select distinct and <= comparisons. In my experience, this problem often arises when there are at most two rows per pair.
This preserves the original values. So, if you start with:
1 2
5 4
You will get that in the result set.
If you don't care about ordering, then many databases support least()/greatest():
select least(a, b) as a, greatest(a, b) as b
from t
group by least(a, b), greatest(a, b);
You can do the same thing with case expressions. Or, more simply as:
select distinct least(a, b) as a, greatest(a, b) as b
from t;
edited Nov 9 at 21:42
answered Nov 9 at 21:34
Gordon Linoff
745k32285390
745k32285390
...for the second statement, would aDISTINCTbe more idiomatic?
– Clockwork-Muse
Nov 9 at 21:37
Neat. For first one maybe s/where a < b/where a <= b/ ?
– jgreve
Nov 9 at 21:38
@jgreve . . . It depends. But I did elaborate in the answer about the assumptions in the current version and how to extend it.
– Gordon Linoff
Nov 9 at 21:43
add a comment |
...for the second statement, would aDISTINCTbe more idiomatic?
– Clockwork-Muse
Nov 9 at 21:37
Neat. For first one maybe s/where a < b/where a <= b/ ?
– jgreve
Nov 9 at 21:38
@jgreve . . . It depends. But I did elaborate in the answer about the assumptions in the current version and how to extend it.
– Gordon Linoff
Nov 9 at 21:43
...for the second statement, would a
DISTINCT be more idiomatic?– Clockwork-Muse
Nov 9 at 21:37
...for the second statement, would a
DISTINCT be more idiomatic?– Clockwork-Muse
Nov 9 at 21:37
Neat. For first one maybe s/where a < b/where a <= b/ ?
– jgreve
Nov 9 at 21:38
Neat. For first one maybe s/where a < b/where a <= b/ ?
– jgreve
Nov 9 at 21:38
@jgreve . . . It depends. But I did elaborate in the answer about the assumptions in the current version and how to extend it.
– Gordon Linoff
Nov 9 at 21:43
@jgreve . . . It depends. But I did elaborate in the answer about the assumptions in the current version and how to extend it.
– Gordon Linoff
Nov 9 at 21:43
add a comment |
up vote
0
down vote
You just need to skip a row if it is a duplicate. 2, 1 and 1, 2 are duplicates of each other in which case we skip 2, 1 as a duplicate of 1, 2.
DECLARE @t TABLE (a INT, b INT);
INSERT INTO @t VALUES
(1, 1),
(1, 2),
(2, 1),
(3, 1);
SELECT DISTINCT a, b
FROM @t AS t
WHERE NOT EXISTS (
SELECT 1
FROM @t AS dup
WHERE a = t.b AND b = t.a AND a < b
)
Result:
| a | b |
|---|---|
| 1 | 1 |
| 1 | 2 |
| 3 | 1 |
add a comment |
up vote
0
down vote
You just need to skip a row if it is a duplicate. 2, 1 and 1, 2 are duplicates of each other in which case we skip 2, 1 as a duplicate of 1, 2.
DECLARE @t TABLE (a INT, b INT);
INSERT INTO @t VALUES
(1, 1),
(1, 2),
(2, 1),
(3, 1);
SELECT DISTINCT a, b
FROM @t AS t
WHERE NOT EXISTS (
SELECT 1
FROM @t AS dup
WHERE a = t.b AND b = t.a AND a < b
)
Result:
| a | b |
|---|---|
| 1 | 1 |
| 1 | 2 |
| 3 | 1 |
add a comment |
up vote
0
down vote
up vote
0
down vote
You just need to skip a row if it is a duplicate. 2, 1 and 1, 2 are duplicates of each other in which case we skip 2, 1 as a duplicate of 1, 2.
DECLARE @t TABLE (a INT, b INT);
INSERT INTO @t VALUES
(1, 1),
(1, 2),
(2, 1),
(3, 1);
SELECT DISTINCT a, b
FROM @t AS t
WHERE NOT EXISTS (
SELECT 1
FROM @t AS dup
WHERE a = t.b AND b = t.a AND a < b
)
Result:
| a | b |
|---|---|
| 1 | 1 |
| 1 | 2 |
| 3 | 1 |
You just need to skip a row if it is a duplicate. 2, 1 and 1, 2 are duplicates of each other in which case we skip 2, 1 as a duplicate of 1, 2.
DECLARE @t TABLE (a INT, b INT);
INSERT INTO @t VALUES
(1, 1),
(1, 2),
(2, 1),
(3, 1);
SELECT DISTINCT a, b
FROM @t AS t
WHERE NOT EXISTS (
SELECT 1
FROM @t AS dup
WHERE a = t.b AND b = t.a AND a < b
)
Result:
| a | b |
|---|---|
| 1 | 1 |
| 1 | 2 |
| 3 | 1 |
edited Nov 9 at 22:05
answered Nov 9 at 21:52
Salman A
171k65328414
171k65328414
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233513%2fsql-get-only-unique-combination-of-two-columns%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Please tag with the database you are using.
– Gordon Linoff
Nov 9 at 21:33