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?










share|improve this question























  • Please tag with the database you are using.
    – Gordon Linoff
    Nov 9 at 21:33














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?










share|improve this question























  • Please tag with the database you are using.
    – Gordon Linoff
    Nov 9 at 21:33












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?










share|improve this question















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-server combinations






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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












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;





share|improve this answer






















  • ...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










  • @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

















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 |





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    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',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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
    );



    );













     

    draft saved


    draft discarded


















    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

























    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;





    share|improve this answer






















    • ...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










    • @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














    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;





    share|improve this answer






















    • ...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










    • @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












    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;





    share|improve this answer














    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;






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 9 at 21:42

























    answered Nov 9 at 21:34









    Gordon Linoff

    745k32285390




    745k32285390











    • ...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










    • @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










    • 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












    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 |





    share|improve this answer


























      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 |





      share|improve this answer
























        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 |





        share|improve this answer














        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 |






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 9 at 22:05

























        answered Nov 9 at 21:52









        Salman A

        171k65328414




        171k65328414



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Kleinkühnau

            Makov (Slowakei)

            Deutsches Schauspielhaus