Completely Unique Rows and Columns in SQL










1















I want to randomly pick 4 rows which are distinct and do not have any entry that matches with any of the 4 chosen columns.



Here is what I coded:



SELECT DISTINCT en,dialect,fr FROM words ORDER BY RANDOM() LIMIT 4


Here is some data:



**en** **dialect** **fr**
number SFA numero
number TRI numero
hotel CAI hotel
hotel SFA hotel


I want:



**en** **dialect** **fr**
number SFA numero
hotel CAI hotel


Some retrieved rows would have something similar with each other, like having the same en or the same fr, I would like to retrieved rows that do not share anything similar with each other, how do I do that?










share|improve this question
























  • I am using sqlite with nodeJS

    – Dark Lotus
    Nov 15 '18 at 5:33











  • Great, now just add sample data to your question.

    – Tim Biegeleisen
    Nov 15 '18 at 5:34











  • alright I edited the question

    – Dark Lotus
    Nov 15 '18 at 5:42















1















I want to randomly pick 4 rows which are distinct and do not have any entry that matches with any of the 4 chosen columns.



Here is what I coded:



SELECT DISTINCT en,dialect,fr FROM words ORDER BY RANDOM() LIMIT 4


Here is some data:



**en** **dialect** **fr**
number SFA numero
number TRI numero
hotel CAI hotel
hotel SFA hotel


I want:



**en** **dialect** **fr**
number SFA numero
hotel CAI hotel


Some retrieved rows would have something similar with each other, like having the same en or the same fr, I would like to retrieved rows that do not share anything similar with each other, how do I do that?










share|improve this question
























  • I am using sqlite with nodeJS

    – Dark Lotus
    Nov 15 '18 at 5:33











  • Great, now just add sample data to your question.

    – Tim Biegeleisen
    Nov 15 '18 at 5:34











  • alright I edited the question

    – Dark Lotus
    Nov 15 '18 at 5:42













1












1








1








I want to randomly pick 4 rows which are distinct and do not have any entry that matches with any of the 4 chosen columns.



Here is what I coded:



SELECT DISTINCT en,dialect,fr FROM words ORDER BY RANDOM() LIMIT 4


Here is some data:



**en** **dialect** **fr**
number SFA numero
number TRI numero
hotel CAI hotel
hotel SFA hotel


I want:



**en** **dialect** **fr**
number SFA numero
hotel CAI hotel


Some retrieved rows would have something similar with each other, like having the same en or the same fr, I would like to retrieved rows that do not share anything similar with each other, how do I do that?










share|improve this question
















I want to randomly pick 4 rows which are distinct and do not have any entry that matches with any of the 4 chosen columns.



Here is what I coded:



SELECT DISTINCT en,dialect,fr FROM words ORDER BY RANDOM() LIMIT 4


Here is some data:



**en** **dialect** **fr**
number SFA numero
number TRI numero
hotel CAI hotel
hotel SFA hotel


I want:



**en** **dialect** **fr**
number SFA numero
hotel CAI hotel


Some retrieved rows would have something similar with each other, like having the same en or the same fr, I would like to retrieved rows that do not share anything similar with each other, how do I do that?







sql sqlite






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 5:41







Dark Lotus

















asked Nov 15 '18 at 5:30









Dark LotusDark Lotus

146




146












  • I am using sqlite with nodeJS

    – Dark Lotus
    Nov 15 '18 at 5:33











  • Great, now just add sample data to your question.

    – Tim Biegeleisen
    Nov 15 '18 at 5:34











  • alright I edited the question

    – Dark Lotus
    Nov 15 '18 at 5:42

















  • I am using sqlite with nodeJS

    – Dark Lotus
    Nov 15 '18 at 5:33











  • Great, now just add sample data to your question.

    – Tim Biegeleisen
    Nov 15 '18 at 5:34











  • alright I edited the question

    – Dark Lotus
    Nov 15 '18 at 5:42
















I am using sqlite with nodeJS

– Dark Lotus
Nov 15 '18 at 5:33





I am using sqlite with nodeJS

– Dark Lotus
Nov 15 '18 at 5:33













Great, now just add sample data to your question.

– Tim Biegeleisen
Nov 15 '18 at 5:34





Great, now just add sample data to your question.

– Tim Biegeleisen
Nov 15 '18 at 5:34













alright I edited the question

– Dark Lotus
Nov 15 '18 at 5:42





alright I edited the question

– Dark Lotus
Nov 15 '18 at 5:42












2 Answers
2






active

oldest

votes


















0














I think I’d do this in the front end code rather the dB, here’s a pseudo code (don’t know what your node looks like):



var seenEn = “en not in (''“;
var seenFr = “fr not in (''“;

var rows =;

while(rows.length < 4)

var newrow = sqlquery(“SELECT *
FROM table WHERE “ + seenEn + “) and ”
+ seenFr + “) ORDER BY random() LIMIT 1”);
if(!newrow)
break;
rows.push(newrow);
seenEn += “,‘“+ newrow.en + “‘“;
seenFr += “,‘“+ newrow.fr + “‘“;



The loop runs as many times as needed to retrieve 4 rows (or maybe make it a for loop that runs 4 times) unless the query returns null. Each time the query returns the values are added to a list of values we don’t want the query to return again. That list had to start out with some values (null) that are never in the data, to prevent a syntax error when concatenation a comma-value string onto the seenXX variable. Those syntax errors can be avoided in other ways like having a Boolean of “if it’s the first value don’t put the comma” but I chose to put dummy ineffective values into the sql to make the JS simpler. Same goes for the



As noted, it looks like JS to ease your understanding but this should be treated as pseudo code outlining a general algorithm - it’s never been compiled/run/tested and may have syntax errors or not at all work as JS if pasted into your file; take the idea and work it into your solution



Please note this was posted from an iphone and it may have done something stupid with all the apostrophes and quotes (turned them into the curly kind preferred by writers rather than the straight kind used by programmers)






share|improve this answer

























  • This is awesome! thanks!

    – Dark Lotus
    Nov 15 '18 at 6:16


















0














You can use Rank or find first row for each group to achieve your result,



Check below , I hope this code will help you



SELECT 'number' AS Col1, 'SFA' AS Col2, 'numero' AS Col3 INTO #tbl
UNION ALL
SELECT 'number','TRI','numero'
UNION ALL
SELECT 'hotel','CAI' ,'hotel'
UNION ALL
SELECT 'hotel','SFA','hotel'
UNION ALL
SELECT 'Location','LocationA' ,'Location data'
UNION ALL
SELECT 'Location','LocationB','Location data'
;
WITH summary AS (
SELECT Col1,Col2,Col3,
ROW_NUMBER() OVER(PARTITION BY p.Col1 ORDER BY p.Col2 DESC) AS rk
FROM #tbl p)
SELECT s.Col1,s.Col2,s.Col3
FROM summary s
WHERE s.rk = 1



DROP TABLE #tbl






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',
    autoActivateHeartbeat: false,
    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%2f53312996%2fcompletely-unique-rows-and-columns-in-sql%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









    0














    I think I’d do this in the front end code rather the dB, here’s a pseudo code (don’t know what your node looks like):



    var seenEn = “en not in (''“;
    var seenFr = “fr not in (''“;

    var rows =;

    while(rows.length < 4)

    var newrow = sqlquery(“SELECT *
    FROM table WHERE “ + seenEn + “) and ”
    + seenFr + “) ORDER BY random() LIMIT 1”);
    if(!newrow)
    break;
    rows.push(newrow);
    seenEn += “,‘“+ newrow.en + “‘“;
    seenFr += “,‘“+ newrow.fr + “‘“;



    The loop runs as many times as needed to retrieve 4 rows (or maybe make it a for loop that runs 4 times) unless the query returns null. Each time the query returns the values are added to a list of values we don’t want the query to return again. That list had to start out with some values (null) that are never in the data, to prevent a syntax error when concatenation a comma-value string onto the seenXX variable. Those syntax errors can be avoided in other ways like having a Boolean of “if it’s the first value don’t put the comma” but I chose to put dummy ineffective values into the sql to make the JS simpler. Same goes for the



    As noted, it looks like JS to ease your understanding but this should be treated as pseudo code outlining a general algorithm - it’s never been compiled/run/tested and may have syntax errors or not at all work as JS if pasted into your file; take the idea and work it into your solution



    Please note this was posted from an iphone and it may have done something stupid with all the apostrophes and quotes (turned them into the curly kind preferred by writers rather than the straight kind used by programmers)






    share|improve this answer

























    • This is awesome! thanks!

      – Dark Lotus
      Nov 15 '18 at 6:16















    0














    I think I’d do this in the front end code rather the dB, here’s a pseudo code (don’t know what your node looks like):



    var seenEn = “en not in (''“;
    var seenFr = “fr not in (''“;

    var rows =;

    while(rows.length < 4)

    var newrow = sqlquery(“SELECT *
    FROM table WHERE “ + seenEn + “) and ”
    + seenFr + “) ORDER BY random() LIMIT 1”);
    if(!newrow)
    break;
    rows.push(newrow);
    seenEn += “,‘“+ newrow.en + “‘“;
    seenFr += “,‘“+ newrow.fr + “‘“;



    The loop runs as many times as needed to retrieve 4 rows (or maybe make it a for loop that runs 4 times) unless the query returns null. Each time the query returns the values are added to a list of values we don’t want the query to return again. That list had to start out with some values (null) that are never in the data, to prevent a syntax error when concatenation a comma-value string onto the seenXX variable. Those syntax errors can be avoided in other ways like having a Boolean of “if it’s the first value don’t put the comma” but I chose to put dummy ineffective values into the sql to make the JS simpler. Same goes for the



    As noted, it looks like JS to ease your understanding but this should be treated as pseudo code outlining a general algorithm - it’s never been compiled/run/tested and may have syntax errors or not at all work as JS if pasted into your file; take the idea and work it into your solution



    Please note this was posted from an iphone and it may have done something stupid with all the apostrophes and quotes (turned them into the curly kind preferred by writers rather than the straight kind used by programmers)






    share|improve this answer

























    • This is awesome! thanks!

      – Dark Lotus
      Nov 15 '18 at 6:16













    0












    0








    0







    I think I’d do this in the front end code rather the dB, here’s a pseudo code (don’t know what your node looks like):



    var seenEn = “en not in (''“;
    var seenFr = “fr not in (''“;

    var rows =;

    while(rows.length < 4)

    var newrow = sqlquery(“SELECT *
    FROM table WHERE “ + seenEn + “) and ”
    + seenFr + “) ORDER BY random() LIMIT 1”);
    if(!newrow)
    break;
    rows.push(newrow);
    seenEn += “,‘“+ newrow.en + “‘“;
    seenFr += “,‘“+ newrow.fr + “‘“;



    The loop runs as many times as needed to retrieve 4 rows (or maybe make it a for loop that runs 4 times) unless the query returns null. Each time the query returns the values are added to a list of values we don’t want the query to return again. That list had to start out with some values (null) that are never in the data, to prevent a syntax error when concatenation a comma-value string onto the seenXX variable. Those syntax errors can be avoided in other ways like having a Boolean of “if it’s the first value don’t put the comma” but I chose to put dummy ineffective values into the sql to make the JS simpler. Same goes for the



    As noted, it looks like JS to ease your understanding but this should be treated as pseudo code outlining a general algorithm - it’s never been compiled/run/tested and may have syntax errors or not at all work as JS if pasted into your file; take the idea and work it into your solution



    Please note this was posted from an iphone and it may have done something stupid with all the apostrophes and quotes (turned them into the curly kind preferred by writers rather than the straight kind used by programmers)






    share|improve this answer















    I think I’d do this in the front end code rather the dB, here’s a pseudo code (don’t know what your node looks like):



    var seenEn = “en not in (''“;
    var seenFr = “fr not in (''“;

    var rows =;

    while(rows.length < 4)

    var newrow = sqlquery(“SELECT *
    FROM table WHERE “ + seenEn + “) and ”
    + seenFr + “) ORDER BY random() LIMIT 1”);
    if(!newrow)
    break;
    rows.push(newrow);
    seenEn += “,‘“+ newrow.en + “‘“;
    seenFr += “,‘“+ newrow.fr + “‘“;



    The loop runs as many times as needed to retrieve 4 rows (or maybe make it a for loop that runs 4 times) unless the query returns null. Each time the query returns the values are added to a list of values we don’t want the query to return again. That list had to start out with some values (null) that are never in the data, to prevent a syntax error when concatenation a comma-value string onto the seenXX variable. Those syntax errors can be avoided in other ways like having a Boolean of “if it’s the first value don’t put the comma” but I chose to put dummy ineffective values into the sql to make the JS simpler. Same goes for the



    As noted, it looks like JS to ease your understanding but this should be treated as pseudo code outlining a general algorithm - it’s never been compiled/run/tested and may have syntax errors or not at all work as JS if pasted into your file; take the idea and work it into your solution



    Please note this was posted from an iphone and it may have done something stupid with all the apostrophes and quotes (turned them into the curly kind preferred by writers rather than the straight kind used by programmers)







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 15 '18 at 6:21

























    answered Nov 15 '18 at 6:10









    Caius JardCaius Jard

    12.5k21340




    12.5k21340












    • This is awesome! thanks!

      – Dark Lotus
      Nov 15 '18 at 6:16

















    • This is awesome! thanks!

      – Dark Lotus
      Nov 15 '18 at 6:16
















    This is awesome! thanks!

    – Dark Lotus
    Nov 15 '18 at 6:16





    This is awesome! thanks!

    – Dark Lotus
    Nov 15 '18 at 6:16













    0














    You can use Rank or find first row for each group to achieve your result,



    Check below , I hope this code will help you



    SELECT 'number' AS Col1, 'SFA' AS Col2, 'numero' AS Col3 INTO #tbl
    UNION ALL
    SELECT 'number','TRI','numero'
    UNION ALL
    SELECT 'hotel','CAI' ,'hotel'
    UNION ALL
    SELECT 'hotel','SFA','hotel'
    UNION ALL
    SELECT 'Location','LocationA' ,'Location data'
    UNION ALL
    SELECT 'Location','LocationB','Location data'
    ;
    WITH summary AS (
    SELECT Col1,Col2,Col3,
    ROW_NUMBER() OVER(PARTITION BY p.Col1 ORDER BY p.Col2 DESC) AS rk
    FROM #tbl p)
    SELECT s.Col1,s.Col2,s.Col3
    FROM summary s
    WHERE s.rk = 1



    DROP TABLE #tbl






    share|improve this answer



























      0














      You can use Rank or find first row for each group to achieve your result,



      Check below , I hope this code will help you



      SELECT 'number' AS Col1, 'SFA' AS Col2, 'numero' AS Col3 INTO #tbl
      UNION ALL
      SELECT 'number','TRI','numero'
      UNION ALL
      SELECT 'hotel','CAI' ,'hotel'
      UNION ALL
      SELECT 'hotel','SFA','hotel'
      UNION ALL
      SELECT 'Location','LocationA' ,'Location data'
      UNION ALL
      SELECT 'Location','LocationB','Location data'
      ;
      WITH summary AS (
      SELECT Col1,Col2,Col3,
      ROW_NUMBER() OVER(PARTITION BY p.Col1 ORDER BY p.Col2 DESC) AS rk
      FROM #tbl p)
      SELECT s.Col1,s.Col2,s.Col3
      FROM summary s
      WHERE s.rk = 1



      DROP TABLE #tbl






      share|improve this answer

























        0












        0








        0







        You can use Rank or find first row for each group to achieve your result,



        Check below , I hope this code will help you



        SELECT 'number' AS Col1, 'SFA' AS Col2, 'numero' AS Col3 INTO #tbl
        UNION ALL
        SELECT 'number','TRI','numero'
        UNION ALL
        SELECT 'hotel','CAI' ,'hotel'
        UNION ALL
        SELECT 'hotel','SFA','hotel'
        UNION ALL
        SELECT 'Location','LocationA' ,'Location data'
        UNION ALL
        SELECT 'Location','LocationB','Location data'
        ;
        WITH summary AS (
        SELECT Col1,Col2,Col3,
        ROW_NUMBER() OVER(PARTITION BY p.Col1 ORDER BY p.Col2 DESC) AS rk
        FROM #tbl p)
        SELECT s.Col1,s.Col2,s.Col3
        FROM summary s
        WHERE s.rk = 1



        DROP TABLE #tbl






        share|improve this answer













        You can use Rank or find first row for each group to achieve your result,



        Check below , I hope this code will help you



        SELECT 'number' AS Col1, 'SFA' AS Col2, 'numero' AS Col3 INTO #tbl
        UNION ALL
        SELECT 'number','TRI','numero'
        UNION ALL
        SELECT 'hotel','CAI' ,'hotel'
        UNION ALL
        SELECT 'hotel','SFA','hotel'
        UNION ALL
        SELECT 'Location','LocationA' ,'Location data'
        UNION ALL
        SELECT 'Location','LocationB','Location data'
        ;
        WITH summary AS (
        SELECT Col1,Col2,Col3,
        ROW_NUMBER() OVER(PARTITION BY p.Col1 ORDER BY p.Col2 DESC) AS rk
        FROM #tbl p)
        SELECT s.Col1,s.Col2,s.Col3
        FROM summary s
        WHERE s.rk = 1



        DROP TABLE #tbl







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 6:14









        Ramakrishnan SaminathanRamakrishnan Saminathan

        11




        11



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


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




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312996%2fcompletely-unique-rows-and-columns-in-sql%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