Extract tuples with specified common values in another column in SQL

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite












I have a dataset that look like:



 Col1 Col2 
1 ABC
2 DEF
3 ABC
1 DEF


Expected output:



Col1 Col2 
1 ABC
1 DEF


I want to extract only those IDSs from Col1 which have both values ABC and DEF in the column.



I tried the self-join in SQL but that did not give me the expected result.



SELECT DISTINCT Col1
FROM db A, db B
WHERE A.ID <> B.ID
AND A.Col2 = 'ABC'
AND B.Col2 = 'DEF'
GROUP BY A.Col1


Also, I tried to the same thing in R using the following code:



vc <- c("ABC", "DEF")
data1 <- db[db$Col2 %in% vc,]


Again, I did not get the desired output. Thanks for all the pointers in advance.










share|improve this question









New contributor




Priyanka Indapurkar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.























    up vote
    6
    down vote

    favorite












    I have a dataset that look like:



     Col1 Col2 
    1 ABC
    2 DEF
    3 ABC
    1 DEF


    Expected output:



    Col1 Col2 
    1 ABC
    1 DEF


    I want to extract only those IDSs from Col1 which have both values ABC and DEF in the column.



    I tried the self-join in SQL but that did not give me the expected result.



    SELECT DISTINCT Col1
    FROM db A, db B
    WHERE A.ID <> B.ID
    AND A.Col2 = 'ABC'
    AND B.Col2 = 'DEF'
    GROUP BY A.Col1


    Also, I tried to the same thing in R using the following code:



    vc <- c("ABC", "DEF")
    data1 <- db[db$Col2 %in% vc,]


    Again, I did not get the desired output. Thanks for all the pointers in advance.










    share|improve this question









    New contributor




    Priyanka Indapurkar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      6
      down vote

      favorite









      up vote
      6
      down vote

      favorite











      I have a dataset that look like:



       Col1 Col2 
      1 ABC
      2 DEF
      3 ABC
      1 DEF


      Expected output:



      Col1 Col2 
      1 ABC
      1 DEF


      I want to extract only those IDSs from Col1 which have both values ABC and DEF in the column.



      I tried the self-join in SQL but that did not give me the expected result.



      SELECT DISTINCT Col1
      FROM db A, db B
      WHERE A.ID <> B.ID
      AND A.Col2 = 'ABC'
      AND B.Col2 = 'DEF'
      GROUP BY A.Col1


      Also, I tried to the same thing in R using the following code:



      vc <- c("ABC", "DEF")
      data1 <- db[db$Col2 %in% vc,]


      Again, I did not get the desired output. Thanks for all the pointers in advance.










      share|improve this question









      New contributor




      Priyanka Indapurkar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I have a dataset that look like:



       Col1 Col2 
      1 ABC
      2 DEF
      3 ABC
      1 DEF


      Expected output:



      Col1 Col2 
      1 ABC
      1 DEF


      I want to extract only those IDSs from Col1 which have both values ABC and DEF in the column.



      I tried the self-join in SQL but that did not give me the expected result.



      SELECT DISTINCT Col1
      FROM db A, db B
      WHERE A.ID <> B.ID
      AND A.Col2 = 'ABC'
      AND B.Col2 = 'DEF'
      GROUP BY A.Col1


      Also, I tried to the same thing in R using the following code:



      vc <- c("ABC", "DEF")
      data1 <- db[db$Col2 %in% vc,]


      Again, I did not get the desired output. Thanks for all the pointers in advance.







      mysql r






      share|improve this question









      New contributor




      Priyanka Indapurkar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Priyanka Indapurkar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 3 hours ago









      Tim Biegeleisen

      197k1377125




      197k1377125






      New contributor




      Priyanka Indapurkar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 3 hours ago









      Priyanka Indapurkar

      312




      312




      New contributor




      Priyanka Indapurkar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Priyanka Indapurkar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Priyanka Indapurkar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          6
          down vote













          In R, you could do



          library(dplyr) 
          df %>%
          group_by(Col1) %>%
          filter(all(vc %in% Col2))

          # Col1 Col2
          # <int> <fct>
          #1 1 ABC
          #2 1 DEF



          The Base R equivalent of that would be



          df[as.logical(with(df, ave(Col2, Col1, FUN = function(x) all(vc %in% x)))), ]

          # Col1 Col2
          #1 1 ABC
          #4 1 DEF


          We select the groups which has all of vc in them.






          share|improve this answer





























            up vote
            3
            down vote













            Here is your current query corrected:



            SELECT DISTINCT t1.Col1
            FROM yourTable t1
            INNER JOIN yourTable t2
            ON t1.Col1 = t2.Col1
            WHERE t1.Col2 = 'ABC' AND t2.Col2 = 'DEF';



            Demo



            The join condition is that both Col1 values are the same, the first Col2 value is ABC and the second Col2 value is DEF.



            But, I would probably use the following canonical approach to this:



            SELECT Col1
            FROM yourTable
            WHERE Col2 IN ('ABC', 'DEF')
            GROUP BY Col1
            HAVING MIN(Col2) <> MAX(Col2);





            share|improve this answer



























              up vote
              0
              down vote













              Use correlated subquery:



              select * from tablename t 
              where exists (select 1 from tablename t1 where t1.col1=t.col1 and col2 in ('ABC','DEF')
              group by col1 having count(distinct col2)=2)





              share|improve this answer



























                up vote
                0
                down vote













                Here's a way using group_concat



                select t.Col1,t.col2
                from t
                join
                (
                select col1,group_concat(distinct col2 order by col2) gc
                from t
                group by col1 having gc = 'abc,def'
                ) s
                on s.col1 = t.col1;

                +------+------+
                | Col1 | col2 |
                +------+------+
                | 1 | ABC |
                | 1 | DEF |
                +------+------+
                2 rows in set (0.16 sec)


                But you do have to understand the order that col2 will be in






                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: false,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  );



                  );






                  Priyanka Indapurkar is a new contributor. Be nice, and check out our Code of Conduct.









                   

                  draft saved


                  draft discarded


















                  StackExchange.ready(
                  function ()
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52620832%2fextract-tuples-with-specified-common-values-in-another-column-in-sql%23new-answer', 'question_page');

                  );

                  Post as a guest






























                  4 Answers
                  4






                  active

                  oldest

                  votes








                  4 Answers
                  4






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes








                  up vote
                  6
                  down vote













                  In R, you could do



                  library(dplyr) 
                  df %>%
                  group_by(Col1) %>%
                  filter(all(vc %in% Col2))

                  # Col1 Col2
                  # <int> <fct>
                  #1 1 ABC
                  #2 1 DEF



                  The Base R equivalent of that would be



                  df[as.logical(with(df, ave(Col2, Col1, FUN = function(x) all(vc %in% x)))), ]

                  # Col1 Col2
                  #1 1 ABC
                  #4 1 DEF


                  We select the groups which has all of vc in them.






                  share|improve this answer


























                    up vote
                    6
                    down vote













                    In R, you could do



                    library(dplyr) 
                    df %>%
                    group_by(Col1) %>%
                    filter(all(vc %in% Col2))

                    # Col1 Col2
                    # <int> <fct>
                    #1 1 ABC
                    #2 1 DEF



                    The Base R equivalent of that would be



                    df[as.logical(with(df, ave(Col2, Col1, FUN = function(x) all(vc %in% x)))), ]

                    # Col1 Col2
                    #1 1 ABC
                    #4 1 DEF


                    We select the groups which has all of vc in them.






                    share|improve this answer
























                      up vote
                      6
                      down vote










                      up vote
                      6
                      down vote









                      In R, you could do



                      library(dplyr) 
                      df %>%
                      group_by(Col1) %>%
                      filter(all(vc %in% Col2))

                      # Col1 Col2
                      # <int> <fct>
                      #1 1 ABC
                      #2 1 DEF



                      The Base R equivalent of that would be



                      df[as.logical(with(df, ave(Col2, Col1, FUN = function(x) all(vc %in% x)))), ]

                      # Col1 Col2
                      #1 1 ABC
                      #4 1 DEF


                      We select the groups which has all of vc in them.






                      share|improve this answer














                      In R, you could do



                      library(dplyr) 
                      df %>%
                      group_by(Col1) %>%
                      filter(all(vc %in% Col2))

                      # Col1 Col2
                      # <int> <fct>
                      #1 1 ABC
                      #2 1 DEF



                      The Base R equivalent of that would be



                      df[as.logical(with(df, ave(Col2, Col1, FUN = function(x) all(vc %in% x)))), ]

                      # Col1 Col2
                      #1 1 ABC
                      #4 1 DEF


                      We select the groups which has all of vc in them.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited 2 hours ago









                      zx8754

                      27.6k76394




                      27.6k76394










                      answered 3 hours ago









                      Ronak Shah

                      24.4k93551




                      24.4k93551






















                          up vote
                          3
                          down vote













                          Here is your current query corrected:



                          SELECT DISTINCT t1.Col1
                          FROM yourTable t1
                          INNER JOIN yourTable t2
                          ON t1.Col1 = t2.Col1
                          WHERE t1.Col2 = 'ABC' AND t2.Col2 = 'DEF';



                          Demo



                          The join condition is that both Col1 values are the same, the first Col2 value is ABC and the second Col2 value is DEF.



                          But, I would probably use the following canonical approach to this:



                          SELECT Col1
                          FROM yourTable
                          WHERE Col2 IN ('ABC', 'DEF')
                          GROUP BY Col1
                          HAVING MIN(Col2) <> MAX(Col2);





                          share|improve this answer
























                            up vote
                            3
                            down vote













                            Here is your current query corrected:



                            SELECT DISTINCT t1.Col1
                            FROM yourTable t1
                            INNER JOIN yourTable t2
                            ON t1.Col1 = t2.Col1
                            WHERE t1.Col2 = 'ABC' AND t2.Col2 = 'DEF';



                            Demo



                            The join condition is that both Col1 values are the same, the first Col2 value is ABC and the second Col2 value is DEF.



                            But, I would probably use the following canonical approach to this:



                            SELECT Col1
                            FROM yourTable
                            WHERE Col2 IN ('ABC', 'DEF')
                            GROUP BY Col1
                            HAVING MIN(Col2) <> MAX(Col2);





                            share|improve this answer






















                              up vote
                              3
                              down vote










                              up vote
                              3
                              down vote









                              Here is your current query corrected:



                              SELECT DISTINCT t1.Col1
                              FROM yourTable t1
                              INNER JOIN yourTable t2
                              ON t1.Col1 = t2.Col1
                              WHERE t1.Col2 = 'ABC' AND t2.Col2 = 'DEF';



                              Demo



                              The join condition is that both Col1 values are the same, the first Col2 value is ABC and the second Col2 value is DEF.



                              But, I would probably use the following canonical approach to this:



                              SELECT Col1
                              FROM yourTable
                              WHERE Col2 IN ('ABC', 'DEF')
                              GROUP BY Col1
                              HAVING MIN(Col2) <> MAX(Col2);





                              share|improve this answer












                              Here is your current query corrected:



                              SELECT DISTINCT t1.Col1
                              FROM yourTable t1
                              INNER JOIN yourTable t2
                              ON t1.Col1 = t2.Col1
                              WHERE t1.Col2 = 'ABC' AND t2.Col2 = 'DEF';



                              Demo



                              The join condition is that both Col1 values are the same, the first Col2 value is ABC and the second Col2 value is DEF.



                              But, I would probably use the following canonical approach to this:



                              SELECT Col1
                              FROM yourTable
                              WHERE Col2 IN ('ABC', 'DEF')
                              GROUP BY Col1
                              HAVING MIN(Col2) <> MAX(Col2);






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered 3 hours ago









                              Tim Biegeleisen

                              197k1377125




                              197k1377125




















                                  up vote
                                  0
                                  down vote













                                  Use correlated subquery:



                                  select * from tablename t 
                                  where exists (select 1 from tablename t1 where t1.col1=t.col1 and col2 in ('ABC','DEF')
                                  group by col1 having count(distinct col2)=2)





                                  share|improve this answer
























                                    up vote
                                    0
                                    down vote













                                    Use correlated subquery:



                                    select * from tablename t 
                                    where exists (select 1 from tablename t1 where t1.col1=t.col1 and col2 in ('ABC','DEF')
                                    group by col1 having count(distinct col2)=2)





                                    share|improve this answer






















                                      up vote
                                      0
                                      down vote










                                      up vote
                                      0
                                      down vote









                                      Use correlated subquery:



                                      select * from tablename t 
                                      where exists (select 1 from tablename t1 where t1.col1=t.col1 and col2 in ('ABC','DEF')
                                      group by col1 having count(distinct col2)=2)





                                      share|improve this answer












                                      Use correlated subquery:



                                      select * from tablename t 
                                      where exists (select 1 from tablename t1 where t1.col1=t.col1 and col2 in ('ABC','DEF')
                                      group by col1 having count(distinct col2)=2)






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered 3 hours ago









                                      fa06

                                      4,612413




                                      4,612413




















                                          up vote
                                          0
                                          down vote













                                          Here's a way using group_concat



                                          select t.Col1,t.col2
                                          from t
                                          join
                                          (
                                          select col1,group_concat(distinct col2 order by col2) gc
                                          from t
                                          group by col1 having gc = 'abc,def'
                                          ) s
                                          on s.col1 = t.col1;

                                          +------+------+
                                          | Col1 | col2 |
                                          +------+------+
                                          | 1 | ABC |
                                          | 1 | DEF |
                                          +------+------+
                                          2 rows in set (0.16 sec)


                                          But you do have to understand the order that col2 will be in






                                          share|improve this answer
























                                            up vote
                                            0
                                            down vote













                                            Here's a way using group_concat



                                            select t.Col1,t.col2
                                            from t
                                            join
                                            (
                                            select col1,group_concat(distinct col2 order by col2) gc
                                            from t
                                            group by col1 having gc = 'abc,def'
                                            ) s
                                            on s.col1 = t.col1;

                                            +------+------+
                                            | Col1 | col2 |
                                            +------+------+
                                            | 1 | ABC |
                                            | 1 | DEF |
                                            +------+------+
                                            2 rows in set (0.16 sec)


                                            But you do have to understand the order that col2 will be in






                                            share|improve this answer






















                                              up vote
                                              0
                                              down vote










                                              up vote
                                              0
                                              down vote









                                              Here's a way using group_concat



                                              select t.Col1,t.col2
                                              from t
                                              join
                                              (
                                              select col1,group_concat(distinct col2 order by col2) gc
                                              from t
                                              group by col1 having gc = 'abc,def'
                                              ) s
                                              on s.col1 = t.col1;

                                              +------+------+
                                              | Col1 | col2 |
                                              +------+------+
                                              | 1 | ABC |
                                              | 1 | DEF |
                                              +------+------+
                                              2 rows in set (0.16 sec)


                                              But you do have to understand the order that col2 will be in






                                              share|improve this answer












                                              Here's a way using group_concat



                                              select t.Col1,t.col2
                                              from t
                                              join
                                              (
                                              select col1,group_concat(distinct col2 order by col2) gc
                                              from t
                                              group by col1 having gc = 'abc,def'
                                              ) s
                                              on s.col1 = t.col1;

                                              +------+------+
                                              | Col1 | col2 |
                                              +------+------+
                                              | 1 | ABC |
                                              | 1 | DEF |
                                              +------+------+
                                              2 rows in set (0.16 sec)


                                              But you do have to understand the order that col2 will be in







                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered 3 hours ago









                                              P.Salmon

                                              6,9472415




                                              6,9472415




















                                                  Priyanka Indapurkar is a new contributor. Be nice, and check out our Code of Conduct.









                                                   

                                                  draft saved


                                                  draft discarded


















                                                  Priyanka Indapurkar is a new contributor. Be nice, and check out our Code of Conduct.












                                                  Priyanka Indapurkar is a new contributor. Be nice, and check out our Code of Conduct.











                                                  Priyanka Indapurkar is a new contributor. Be nice, and check out our Code of Conduct.













                                                   


                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function ()
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52620832%2fextract-tuples-with-specified-common-values-in-another-column-in-sql%23new-answer', 'question_page');

                                                  );

                                                  Post as a guest













































































                                                  Comments

                                                  Popular posts from this blog

                                                  What does second last employer means? [closed]

                                                  List of Gilmore Girls characters

                                                  Confectionery