SQLite query - filter name where each associated id is contained within a set of ids










0















I'm trying to work out a query that will find me all of the distinct Names whose LocationIDs are in a given set of ids. The catch is if any of the LocationIDs associated with a distinct Name are not in the set, then the Name should not be in the results.



Say I have the following table:



ID | LocationID | ... | Name
-----------------------------
1 | 1 | ... | A
2 | 1 | ... | B
3 | 2 | ... | B


I'm needing a query similar to



SELECT DISTINCT Name FROM table WHERE LocationID IN (1, 2);


The problem with the above is it's just checking if the LocationID is 1 OR 2, this would return the following:



A
B


But what I need it to return is



B


Since B is the only Name where both of its LocationIDs are in the set (1, 2)










share|improve this question


























    0















    I'm trying to work out a query that will find me all of the distinct Names whose LocationIDs are in a given set of ids. The catch is if any of the LocationIDs associated with a distinct Name are not in the set, then the Name should not be in the results.



    Say I have the following table:



    ID | LocationID | ... | Name
    -----------------------------
    1 | 1 | ... | A
    2 | 1 | ... | B
    3 | 2 | ... | B


    I'm needing a query similar to



    SELECT DISTINCT Name FROM table WHERE LocationID IN (1, 2);


    The problem with the above is it's just checking if the LocationID is 1 OR 2, this would return the following:



    A
    B


    But what I need it to return is



    B


    Since B is the only Name where both of its LocationIDs are in the set (1, 2)










    share|improve this question
























      0












      0








      0








      I'm trying to work out a query that will find me all of the distinct Names whose LocationIDs are in a given set of ids. The catch is if any of the LocationIDs associated with a distinct Name are not in the set, then the Name should not be in the results.



      Say I have the following table:



      ID | LocationID | ... | Name
      -----------------------------
      1 | 1 | ... | A
      2 | 1 | ... | B
      3 | 2 | ... | B


      I'm needing a query similar to



      SELECT DISTINCT Name FROM table WHERE LocationID IN (1, 2);


      The problem with the above is it's just checking if the LocationID is 1 OR 2, this would return the following:



      A
      B


      But what I need it to return is



      B


      Since B is the only Name where both of its LocationIDs are in the set (1, 2)










      share|improve this question














      I'm trying to work out a query that will find me all of the distinct Names whose LocationIDs are in a given set of ids. The catch is if any of the LocationIDs associated with a distinct Name are not in the set, then the Name should not be in the results.



      Say I have the following table:



      ID | LocationID | ... | Name
      -----------------------------
      1 | 1 | ... | A
      2 | 1 | ... | B
      3 | 2 | ... | B


      I'm needing a query similar to



      SELECT DISTINCT Name FROM table WHERE LocationID IN (1, 2);


      The problem with the above is it's just checking if the LocationID is 1 OR 2, this would return the following:



      A
      B


      But what I need it to return is



      B


      Since B is the only Name where both of its LocationIDs are in the set (1, 2)







      sql sqlite select






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 0:41









      Programmer001Programmer001

      540623




      540623






















          2 Answers
          2






          active

          oldest

          votes


















          0














          You can try to write two subquery.



          1. get count by each Name

          2. get count by your condition.

          then join them by count amount, which means your need to all match your condition count number.



          Schema (SQLite v3.17)



          CREATE TABLE T(
          ID int,
          LocationID int,
          Name varchar(5)

          );

          INSERT INTO T VALUES (1, 1,'A');
          INSERT INTO T VALUES (2, 1,'B');
          INSERT INTO T VALUES (3, 2,'B');



          Query #1



          SELECT t2.Name
          FROM
          (
          SELECT COUNT(DISTINCT LocationID) cnt
          FROM T
          WHERE LocationID IN (1, 2)
          ) t1
          JOIN
          (
          SELECT COUNT(DISTINCT LocationID) cnt,Name
          FROM T
          WHERE LocationID IN (1, 2)
          GROUP BY Name
          ) t2 on t1.cnt = t2.cnt;

          | Name |
          | ---- |
          | B |



          View on DB Fiddle






          share|improve this answer

























          • @Programmer001 Is that work for you?

            – D-Shih
            Nov 14 '18 at 1:29











          • Hey, I was just trying it out. It doesn't look like it works for other data sets. Trying it out with actual data returned the wrong results. I added more variety to the data in the fiddle and it returned a name that wasn't in the ID set. I updated the fiddle with a different data set: db-fiddle.com/f/sswND35ZvSb482v7dNQTi5/2 I think we're on the right track for a correct solution but I think comparing by count this way is throwing it off.

            – Programmer001
            Nov 14 '18 at 1:46







          • 1





            @Programmer001 Ok I see did you want to get emty result from your sample data in the sqlfiddle db-fiddle.com/f/sswND35ZvSb482v7dNQTi5/3?

            – D-Shih
            Nov 14 '18 at 1:56






          • 1





            My bad, I added INSERT INTO T VALUES (8, 4, 'A1'); to the schema. This appears to be working. I'm going to keep playing around with it, if nothing pops up then later tomorrow I'll mark this as the accepted answer. Thanks!

            – Programmer001
            Nov 14 '18 at 2:13


















          0














          You can just use aggregation. Assuming no duplicates in your table:



          SELECT Name
          FROM table
          WHERE LocationID IN (1, 2)
          GROUP BY Name
          HAVING COUNT(*) = 2;


          If Name/LocationID pairs can be duplicated, use HAVING COUNT(DISTINCT LocationID) = 2.






          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%2f53291543%2fsqlite-query-filter-name-where-each-associated-id-is-contained-within-a-set-of%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














            You can try to write two subquery.



            1. get count by each Name

            2. get count by your condition.

            then join them by count amount, which means your need to all match your condition count number.



            Schema (SQLite v3.17)



            CREATE TABLE T(
            ID int,
            LocationID int,
            Name varchar(5)

            );

            INSERT INTO T VALUES (1, 1,'A');
            INSERT INTO T VALUES (2, 1,'B');
            INSERT INTO T VALUES (3, 2,'B');



            Query #1



            SELECT t2.Name
            FROM
            (
            SELECT COUNT(DISTINCT LocationID) cnt
            FROM T
            WHERE LocationID IN (1, 2)
            ) t1
            JOIN
            (
            SELECT COUNT(DISTINCT LocationID) cnt,Name
            FROM T
            WHERE LocationID IN (1, 2)
            GROUP BY Name
            ) t2 on t1.cnt = t2.cnt;

            | Name |
            | ---- |
            | B |



            View on DB Fiddle






            share|improve this answer

























            • @Programmer001 Is that work for you?

              – D-Shih
              Nov 14 '18 at 1:29











            • Hey, I was just trying it out. It doesn't look like it works for other data sets. Trying it out with actual data returned the wrong results. I added more variety to the data in the fiddle and it returned a name that wasn't in the ID set. I updated the fiddle with a different data set: db-fiddle.com/f/sswND35ZvSb482v7dNQTi5/2 I think we're on the right track for a correct solution but I think comparing by count this way is throwing it off.

              – Programmer001
              Nov 14 '18 at 1:46







            • 1





              @Programmer001 Ok I see did you want to get emty result from your sample data in the sqlfiddle db-fiddle.com/f/sswND35ZvSb482v7dNQTi5/3?

              – D-Shih
              Nov 14 '18 at 1:56






            • 1





              My bad, I added INSERT INTO T VALUES (8, 4, 'A1'); to the schema. This appears to be working. I'm going to keep playing around with it, if nothing pops up then later tomorrow I'll mark this as the accepted answer. Thanks!

              – Programmer001
              Nov 14 '18 at 2:13















            0














            You can try to write two subquery.



            1. get count by each Name

            2. get count by your condition.

            then join them by count amount, which means your need to all match your condition count number.



            Schema (SQLite v3.17)



            CREATE TABLE T(
            ID int,
            LocationID int,
            Name varchar(5)

            );

            INSERT INTO T VALUES (1, 1,'A');
            INSERT INTO T VALUES (2, 1,'B');
            INSERT INTO T VALUES (3, 2,'B');



            Query #1



            SELECT t2.Name
            FROM
            (
            SELECT COUNT(DISTINCT LocationID) cnt
            FROM T
            WHERE LocationID IN (1, 2)
            ) t1
            JOIN
            (
            SELECT COUNT(DISTINCT LocationID) cnt,Name
            FROM T
            WHERE LocationID IN (1, 2)
            GROUP BY Name
            ) t2 on t1.cnt = t2.cnt;

            | Name |
            | ---- |
            | B |



            View on DB Fiddle






            share|improve this answer

























            • @Programmer001 Is that work for you?

              – D-Shih
              Nov 14 '18 at 1:29











            • Hey, I was just trying it out. It doesn't look like it works for other data sets. Trying it out with actual data returned the wrong results. I added more variety to the data in the fiddle and it returned a name that wasn't in the ID set. I updated the fiddle with a different data set: db-fiddle.com/f/sswND35ZvSb482v7dNQTi5/2 I think we're on the right track for a correct solution but I think comparing by count this way is throwing it off.

              – Programmer001
              Nov 14 '18 at 1:46







            • 1





              @Programmer001 Ok I see did you want to get emty result from your sample data in the sqlfiddle db-fiddle.com/f/sswND35ZvSb482v7dNQTi5/3?

              – D-Shih
              Nov 14 '18 at 1:56






            • 1





              My bad, I added INSERT INTO T VALUES (8, 4, 'A1'); to the schema. This appears to be working. I'm going to keep playing around with it, if nothing pops up then later tomorrow I'll mark this as the accepted answer. Thanks!

              – Programmer001
              Nov 14 '18 at 2:13













            0












            0








            0







            You can try to write two subquery.



            1. get count by each Name

            2. get count by your condition.

            then join them by count amount, which means your need to all match your condition count number.



            Schema (SQLite v3.17)



            CREATE TABLE T(
            ID int,
            LocationID int,
            Name varchar(5)

            );

            INSERT INTO T VALUES (1, 1,'A');
            INSERT INTO T VALUES (2, 1,'B');
            INSERT INTO T VALUES (3, 2,'B');



            Query #1



            SELECT t2.Name
            FROM
            (
            SELECT COUNT(DISTINCT LocationID) cnt
            FROM T
            WHERE LocationID IN (1, 2)
            ) t1
            JOIN
            (
            SELECT COUNT(DISTINCT LocationID) cnt,Name
            FROM T
            WHERE LocationID IN (1, 2)
            GROUP BY Name
            ) t2 on t1.cnt = t2.cnt;

            | Name |
            | ---- |
            | B |



            View on DB Fiddle






            share|improve this answer















            You can try to write two subquery.



            1. get count by each Name

            2. get count by your condition.

            then join them by count amount, which means your need to all match your condition count number.



            Schema (SQLite v3.17)



            CREATE TABLE T(
            ID int,
            LocationID int,
            Name varchar(5)

            );

            INSERT INTO T VALUES (1, 1,'A');
            INSERT INTO T VALUES (2, 1,'B');
            INSERT INTO T VALUES (3, 2,'B');



            Query #1



            SELECT t2.Name
            FROM
            (
            SELECT COUNT(DISTINCT LocationID) cnt
            FROM T
            WHERE LocationID IN (1, 2)
            ) t1
            JOIN
            (
            SELECT COUNT(DISTINCT LocationID) cnt,Name
            FROM T
            WHERE LocationID IN (1, 2)
            GROUP BY Name
            ) t2 on t1.cnt = t2.cnt;

            | Name |
            | ---- |
            | B |



            View on DB Fiddle







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 14 '18 at 1:56

























            answered Nov 14 '18 at 0:44









            D-ShihD-Shih

            25.8k61531




            25.8k61531












            • @Programmer001 Is that work for you?

              – D-Shih
              Nov 14 '18 at 1:29











            • Hey, I was just trying it out. It doesn't look like it works for other data sets. Trying it out with actual data returned the wrong results. I added more variety to the data in the fiddle and it returned a name that wasn't in the ID set. I updated the fiddle with a different data set: db-fiddle.com/f/sswND35ZvSb482v7dNQTi5/2 I think we're on the right track for a correct solution but I think comparing by count this way is throwing it off.

              – Programmer001
              Nov 14 '18 at 1:46







            • 1





              @Programmer001 Ok I see did you want to get emty result from your sample data in the sqlfiddle db-fiddle.com/f/sswND35ZvSb482v7dNQTi5/3?

              – D-Shih
              Nov 14 '18 at 1:56






            • 1





              My bad, I added INSERT INTO T VALUES (8, 4, 'A1'); to the schema. This appears to be working. I'm going to keep playing around with it, if nothing pops up then later tomorrow I'll mark this as the accepted answer. Thanks!

              – Programmer001
              Nov 14 '18 at 2:13

















            • @Programmer001 Is that work for you?

              – D-Shih
              Nov 14 '18 at 1:29











            • Hey, I was just trying it out. It doesn't look like it works for other data sets. Trying it out with actual data returned the wrong results. I added more variety to the data in the fiddle and it returned a name that wasn't in the ID set. I updated the fiddle with a different data set: db-fiddle.com/f/sswND35ZvSb482v7dNQTi5/2 I think we're on the right track for a correct solution but I think comparing by count this way is throwing it off.

              – Programmer001
              Nov 14 '18 at 1:46







            • 1





              @Programmer001 Ok I see did you want to get emty result from your sample data in the sqlfiddle db-fiddle.com/f/sswND35ZvSb482v7dNQTi5/3?

              – D-Shih
              Nov 14 '18 at 1:56






            • 1





              My bad, I added INSERT INTO T VALUES (8, 4, 'A1'); to the schema. This appears to be working. I'm going to keep playing around with it, if nothing pops up then later tomorrow I'll mark this as the accepted answer. Thanks!

              – Programmer001
              Nov 14 '18 at 2:13
















            @Programmer001 Is that work for you?

            – D-Shih
            Nov 14 '18 at 1:29





            @Programmer001 Is that work for you?

            – D-Shih
            Nov 14 '18 at 1:29













            Hey, I was just trying it out. It doesn't look like it works for other data sets. Trying it out with actual data returned the wrong results. I added more variety to the data in the fiddle and it returned a name that wasn't in the ID set. I updated the fiddle with a different data set: db-fiddle.com/f/sswND35ZvSb482v7dNQTi5/2 I think we're on the right track for a correct solution but I think comparing by count this way is throwing it off.

            – Programmer001
            Nov 14 '18 at 1:46






            Hey, I was just trying it out. It doesn't look like it works for other data sets. Trying it out with actual data returned the wrong results. I added more variety to the data in the fiddle and it returned a name that wasn't in the ID set. I updated the fiddle with a different data set: db-fiddle.com/f/sswND35ZvSb482v7dNQTi5/2 I think we're on the right track for a correct solution but I think comparing by count this way is throwing it off.

            – Programmer001
            Nov 14 '18 at 1:46





            1




            1





            @Programmer001 Ok I see did you want to get emty result from your sample data in the sqlfiddle db-fiddle.com/f/sswND35ZvSb482v7dNQTi5/3?

            – D-Shih
            Nov 14 '18 at 1:56





            @Programmer001 Ok I see did you want to get emty result from your sample data in the sqlfiddle db-fiddle.com/f/sswND35ZvSb482v7dNQTi5/3?

            – D-Shih
            Nov 14 '18 at 1:56




            1




            1





            My bad, I added INSERT INTO T VALUES (8, 4, 'A1'); to the schema. This appears to be working. I'm going to keep playing around with it, if nothing pops up then later tomorrow I'll mark this as the accepted answer. Thanks!

            – Programmer001
            Nov 14 '18 at 2:13





            My bad, I added INSERT INTO T VALUES (8, 4, 'A1'); to the schema. This appears to be working. I'm going to keep playing around with it, if nothing pops up then later tomorrow I'll mark this as the accepted answer. Thanks!

            – Programmer001
            Nov 14 '18 at 2:13













            0














            You can just use aggregation. Assuming no duplicates in your table:



            SELECT Name
            FROM table
            WHERE LocationID IN (1, 2)
            GROUP BY Name
            HAVING COUNT(*) = 2;


            If Name/LocationID pairs can be duplicated, use HAVING COUNT(DISTINCT LocationID) = 2.






            share|improve this answer



























              0














              You can just use aggregation. Assuming no duplicates in your table:



              SELECT Name
              FROM table
              WHERE LocationID IN (1, 2)
              GROUP BY Name
              HAVING COUNT(*) = 2;


              If Name/LocationID pairs can be duplicated, use HAVING COUNT(DISTINCT LocationID) = 2.






              share|improve this answer

























                0












                0








                0







                You can just use aggregation. Assuming no duplicates in your table:



                SELECT Name
                FROM table
                WHERE LocationID IN (1, 2)
                GROUP BY Name
                HAVING COUNT(*) = 2;


                If Name/LocationID pairs can be duplicated, use HAVING COUNT(DISTINCT LocationID) = 2.






                share|improve this answer













                You can just use aggregation. Assuming no duplicates in your table:



                SELECT Name
                FROM table
                WHERE LocationID IN (1, 2)
                GROUP BY Name
                HAVING COUNT(*) = 2;


                If Name/LocationID pairs can be duplicated, use HAVING COUNT(DISTINCT LocationID) = 2.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 14 '18 at 4:01









                Gordon LinoffGordon Linoff

                771k35304406




                771k35304406



























                    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%2f53291543%2fsqlite-query-filter-name-where-each-associated-id-is-contained-within-a-set-of%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







                    這個網誌中的熱門文章

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

                    Museum of Modern and Contemporary Art of Trento and Rovereto