SELECT DISTINCT TWO columns not ranged
How can I select distinct two columns not ranged correctly.
for example:
if I have this table t:
================
|| a || b ||
=======||=======
|| 1 || 2 ||
|| 1 || 3 ||
|| 2 || 3 ||
|| 3 || 1 ||
|| 2 || 1 ||
================
If I choose 1 in my query the result should be:
================
|| a || b ||
=======||=======
|| 1 || 2 ||
|| 1 || 3 ||
================
This table is a result of selecting the rows which has a=1 or b=1 and do not repeat a,b or the inverse.
Details:
The result of SELECT * FROM t WHERE a=1 OR b=1; will be:
================
|| a || b ||
=======||=======
|| 1 || 2 ||
|| 1 || 3 ||
|| 3 || 1 ||
|| 2 || 1 ||
================
I should eliminate the repeated rows normally and inversely.
|| 2 || 1 || will be eliminated because it's the inverse of || 1 || 2 ||
|| 3 || 1 || will be eliminated because it's the inverse of || 1 || 3 ||
The result was shown above.
distinct
bumped to the homepage by Community♦ 45 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
How can I select distinct two columns not ranged correctly.
for example:
if I have this table t:
================
|| a || b ||
=======||=======
|| 1 || 2 ||
|| 1 || 3 ||
|| 2 || 3 ||
|| 3 || 1 ||
|| 2 || 1 ||
================
If I choose 1 in my query the result should be:
================
|| a || b ||
=======||=======
|| 1 || 2 ||
|| 1 || 3 ||
================
This table is a result of selecting the rows which has a=1 or b=1 and do not repeat a,b or the inverse.
Details:
The result of SELECT * FROM t WHERE a=1 OR b=1; will be:
================
|| a || b ||
=======||=======
|| 1 || 2 ||
|| 1 || 3 ||
|| 3 || 1 ||
|| 2 || 1 ||
================
I should eliminate the repeated rows normally and inversely.
|| 2 || 1 || will be eliminated because it's the inverse of || 1 || 2 ||
|| 3 || 1 || will be eliminated because it's the inverse of || 1 || 3 ||
The result was shown above.
distinct
bumped to the homepage by Community♦ 45 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
How can I select distinct two columns not ranged correctly.
for example:
if I have this table t:
================
|| a || b ||
=======||=======
|| 1 || 2 ||
|| 1 || 3 ||
|| 2 || 3 ||
|| 3 || 1 ||
|| 2 || 1 ||
================
If I choose 1 in my query the result should be:
================
|| a || b ||
=======||=======
|| 1 || 2 ||
|| 1 || 3 ||
================
This table is a result of selecting the rows which has a=1 or b=1 and do not repeat a,b or the inverse.
Details:
The result of SELECT * FROM t WHERE a=1 OR b=1; will be:
================
|| a || b ||
=======||=======
|| 1 || 2 ||
|| 1 || 3 ||
|| 3 || 1 ||
|| 2 || 1 ||
================
I should eliminate the repeated rows normally and inversely.
|| 2 || 1 || will be eliminated because it's the inverse of || 1 || 2 ||
|| 3 || 1 || will be eliminated because it's the inverse of || 1 || 3 ||
The result was shown above.
distinct
How can I select distinct two columns not ranged correctly.
for example:
if I have this table t:
================
|| a || b ||
=======||=======
|| 1 || 2 ||
|| 1 || 3 ||
|| 2 || 3 ||
|| 3 || 1 ||
|| 2 || 1 ||
================
If I choose 1 in my query the result should be:
================
|| a || b ||
=======||=======
|| 1 || 2 ||
|| 1 || 3 ||
================
This table is a result of selecting the rows which has a=1 or b=1 and do not repeat a,b or the inverse.
Details:
The result of SELECT * FROM t WHERE a=1 OR b=1; will be:
================
|| a || b ||
=======||=======
|| 1 || 2 ||
|| 1 || 3 ||
|| 3 || 1 ||
|| 2 || 1 ||
================
I should eliminate the repeated rows normally and inversely.
|| 2 || 1 || will be eliminated because it's the inverse of || 1 || 2 ||
|| 3 || 1 || will be eliminated because it's the inverse of || 1 || 3 ||
The result was shown above.
distinct
distinct
asked Mar 9 '16 at 19:43
Safe ModeSafe Mode
1
1
bumped to the homepage by Community♦ 45 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 45 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
So the outputs should be { 1, 2 } and { 1, 3 }? I'm sure there's a more efficient way to do it, but if you just need a quick-and-dirty solution, this should work for you on SQL Server:
SELECT
LEFT(Concatenated, CHARINDEX('|', Concatenated) - 1),
SUBSTRING(Concatenated, CHARINDEX('|', Concatenated) + 1, 999)
FROM
(
SELECT DISTINCT
CASE
WHEN A < B
THEN CAST(A AS VARCHAR(10)) + '|' + CAST(B AS VARCHAR(10))
ELSE CAST(B AS VARCHAR(10)) + '|' + CAST(A AS VARCHAR(10))
END AS Concatenated
FROM
T
WHERE
A = 1 OR B = 1
) AS X
Thank you. give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime.
– Safe Mode
Mar 9 '16 at 20:20
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2fdba.stackexchange.com%2fquestions%2f131743%2fselect-distinct-two-columns-not-ranged%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
So the outputs should be { 1, 2 } and { 1, 3 }? I'm sure there's a more efficient way to do it, but if you just need a quick-and-dirty solution, this should work for you on SQL Server:
SELECT
LEFT(Concatenated, CHARINDEX('|', Concatenated) - 1),
SUBSTRING(Concatenated, CHARINDEX('|', Concatenated) + 1, 999)
FROM
(
SELECT DISTINCT
CASE
WHEN A < B
THEN CAST(A AS VARCHAR(10)) + '|' + CAST(B AS VARCHAR(10))
ELSE CAST(B AS VARCHAR(10)) + '|' + CAST(A AS VARCHAR(10))
END AS Concatenated
FROM
T
WHERE
A = 1 OR B = 1
) AS X
Thank you. give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime.
– Safe Mode
Mar 9 '16 at 20:20
add a comment |
So the outputs should be { 1, 2 } and { 1, 3 }? I'm sure there's a more efficient way to do it, but if you just need a quick-and-dirty solution, this should work for you on SQL Server:
SELECT
LEFT(Concatenated, CHARINDEX('|', Concatenated) - 1),
SUBSTRING(Concatenated, CHARINDEX('|', Concatenated) + 1, 999)
FROM
(
SELECT DISTINCT
CASE
WHEN A < B
THEN CAST(A AS VARCHAR(10)) + '|' + CAST(B AS VARCHAR(10))
ELSE CAST(B AS VARCHAR(10)) + '|' + CAST(A AS VARCHAR(10))
END AS Concatenated
FROM
T
WHERE
A = 1 OR B = 1
) AS X
Thank you. give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime.
– Safe Mode
Mar 9 '16 at 20:20
add a comment |
So the outputs should be { 1, 2 } and { 1, 3 }? I'm sure there's a more efficient way to do it, but if you just need a quick-and-dirty solution, this should work for you on SQL Server:
SELECT
LEFT(Concatenated, CHARINDEX('|', Concatenated) - 1),
SUBSTRING(Concatenated, CHARINDEX('|', Concatenated) + 1, 999)
FROM
(
SELECT DISTINCT
CASE
WHEN A < B
THEN CAST(A AS VARCHAR(10)) + '|' + CAST(B AS VARCHAR(10))
ELSE CAST(B AS VARCHAR(10)) + '|' + CAST(A AS VARCHAR(10))
END AS Concatenated
FROM
T
WHERE
A = 1 OR B = 1
) AS X
So the outputs should be { 1, 2 } and { 1, 3 }? I'm sure there's a more efficient way to do it, but if you just need a quick-and-dirty solution, this should work for you on SQL Server:
SELECT
LEFT(Concatenated, CHARINDEX('|', Concatenated) - 1),
SUBSTRING(Concatenated, CHARINDEX('|', Concatenated) + 1, 999)
FROM
(
SELECT DISTINCT
CASE
WHEN A < B
THEN CAST(A AS VARCHAR(10)) + '|' + CAST(B AS VARCHAR(10))
ELSE CAST(B AS VARCHAR(10)) + '|' + CAST(A AS VARCHAR(10))
END AS Concatenated
FROM
T
WHERE
A = 1 OR B = 1
) AS X
edited Mar 9 '16 at 20:08
Zane
2,65221741
2,65221741
answered Mar 9 '16 at 20:00
Jon of All TradesJon of All Trades
3,23842149
3,23842149
Thank you. give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime.
– Safe Mode
Mar 9 '16 at 20:20
add a comment |
Thank you. give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime.
– Safe Mode
Mar 9 '16 at 20:20
Thank you. give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime.
– Safe Mode
Mar 9 '16 at 20:20
Thank you. give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime.
– Safe Mode
Mar 9 '16 at 20:20
add a comment |
Thanks for contributing an answer to Database Administrators Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2fdba.stackexchange.com%2fquestions%2f131743%2fselect-distinct-two-columns-not-ranged%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