SQL Group By using Two Keys The Next CEO of Stack OverflowHow can I prevent SQL injection in PHP?Add a column with a default value to an existing table in SQL ServerHow to return only the Date from a SQL Server DateTime datatypeInserting multiple rows in a single SQL query?Retrieving the last record in each group - MySQLHow do I UPDATE from a SELECT in SQL Server?Finding duplicate values in a SQL tableSelect first row in each GROUP BY group?Select Distinct 2 ColumnsHow to import an SQL file using the command line in MySQL?

Are there any limitations on attacking while grappling?

How do we know the LHC results are robust?

What connection does MS Office have to Netscape Navigator?

Why do airplanes bank sharply to the right after air-to-air refueling?

MessageLevel in QGIS3

Do I need to enable Dev Hub in my PROD Org?

Interfacing a button to MCU (and PC) with 50m long cable

What was the first Unix version to run on a microcomputer?

How to start emacs in "nothing" mode (`fundamental-mode`)

Would a completely good Muggle be able to use a wand?

Inappropriate reference requests from Journal reviewers

Can you replace a racial trait cantrip when leveling up?

If the heap is initialized for security, then why is the stack uninitialized?

How to avoid supervisors with prejudiced views?

What is the result of assigning to std::vector<T>::begin()?

Received an invoice from my ex-employer billing me for training; how to handle?

WOW air has ceased operation, can I get my tickets refunded?

Does it take more energy to get to Venus or to Mars?

I believe this to be a fraud - hired, then asked to cash check and send cash as Bitcoin

In excess I'm lethal

Why do professional authors make "consistency" mistakes? And how to avoid them?

What is "(CFMCC)" on an ILS approach chart?

What does "Its cash flow is deeply negative" mean?

Why do we use the plural of movies in this phrase "We went to the movies last night."?



SQL Group By using Two Keys



The Next CEO of Stack OverflowHow can I prevent SQL injection in PHP?Add a column with a default value to an existing table in SQL ServerHow to return only the Date from a SQL Server DateTime datatypeInserting multiple rows in a single SQL query?Retrieving the last record in each group - MySQLHow do I UPDATE from a SELECT in SQL Server?Finding duplicate values in a SQL tableSelect first row in each GROUP BY group?Select Distinct 2 ColumnsHow to import an SQL file using the command line in MySQL?










0















I'm looking to write a query to group by ID1, ID2, but only return the IDs where there is >1 unique ID1 for ID2.



I have data like this:



+------+------+
| ID1 | ID2 |
+------+------+
|1 |A |
+------+------+
|1 |A |
+------+------+
|2 |A |
+------+------+
|3 |B |
+------+------+
|3 |B |
+------+------+
|4 |C |
+------+------+
|5 |C |
+------+------+
|6 |D |
+------+------+
|6 |D |
+------+------+
|7 |E |
+------+------+


Ideally, my output will look like this:



+------+
| ID2 |
+------+
|A |
+------+
|C |
+------+


Notice how there are >1 record for ID2 = 'B' or ID2 = 'D', but they have the same ID1. In example A, even though there are duplicate "1" values of ID1 I would still like to select it because there is another Unique ID1 - "2".










share|improve this question


























    0















    I'm looking to write a query to group by ID1, ID2, but only return the IDs where there is >1 unique ID1 for ID2.



    I have data like this:



    +------+------+
    | ID1 | ID2 |
    +------+------+
    |1 |A |
    +------+------+
    |1 |A |
    +------+------+
    |2 |A |
    +------+------+
    |3 |B |
    +------+------+
    |3 |B |
    +------+------+
    |4 |C |
    +------+------+
    |5 |C |
    +------+------+
    |6 |D |
    +------+------+
    |6 |D |
    +------+------+
    |7 |E |
    +------+------+


    Ideally, my output will look like this:



    +------+
    | ID2 |
    +------+
    |A |
    +------+
    |C |
    +------+


    Notice how there are >1 record for ID2 = 'B' or ID2 = 'D', but they have the same ID1. In example A, even though there are duplicate "1" values of ID1 I would still like to select it because there is another Unique ID1 - "2".










    share|improve this question
























      0












      0








      0








      I'm looking to write a query to group by ID1, ID2, but only return the IDs where there is >1 unique ID1 for ID2.



      I have data like this:



      +------+------+
      | ID1 | ID2 |
      +------+------+
      |1 |A |
      +------+------+
      |1 |A |
      +------+------+
      |2 |A |
      +------+------+
      |3 |B |
      +------+------+
      |3 |B |
      +------+------+
      |4 |C |
      +------+------+
      |5 |C |
      +------+------+
      |6 |D |
      +------+------+
      |6 |D |
      +------+------+
      |7 |E |
      +------+------+


      Ideally, my output will look like this:



      +------+
      | ID2 |
      +------+
      |A |
      +------+
      |C |
      +------+


      Notice how there are >1 record for ID2 = 'B' or ID2 = 'D', but they have the same ID1. In example A, even though there are duplicate "1" values of ID1 I would still like to select it because there is another Unique ID1 - "2".










      share|improve this question














      I'm looking to write a query to group by ID1, ID2, but only return the IDs where there is >1 unique ID1 for ID2.



      I have data like this:



      +------+------+
      | ID1 | ID2 |
      +------+------+
      |1 |A |
      +------+------+
      |1 |A |
      +------+------+
      |2 |A |
      +------+------+
      |3 |B |
      +------+------+
      |3 |B |
      +------+------+
      |4 |C |
      +------+------+
      |5 |C |
      +------+------+
      |6 |D |
      +------+------+
      |6 |D |
      +------+------+
      |7 |E |
      +------+------+


      Ideally, my output will look like this:



      +------+
      | ID2 |
      +------+
      |A |
      +------+
      |C |
      +------+


      Notice how there are >1 record for ID2 = 'B' or ID2 = 'D', but they have the same ID1. In example A, even though there are duplicate "1" values of ID1 I would still like to select it because there is another Unique ID1 - "2".







      sql hadoop impala






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 8 at 14:22









      DukeLukeDukeLuke

      206113




      206113






















          3 Answers
          3






          active

          oldest

          votes


















          0














          You can use COUNT(DISTINCT ID1) in the having clause as the following:



          SELECT ID2
          FROM tbl
          GROUP BY sID2
          HAVING COUNT(DISTINCT ID1) > 1





          share|improve this answer






























            0














            select ID2
            from t
            group by ID2
            having count(distinct ID1) > 1


            Not sure if Impala has count(distinct), but this is fairly standard so I am going to assume it. The having clause is applied after the group by, so it only keeps the data you are looking for.






            share|improve this answer






























              0














              I would recommend:



              select ID2
              from t
              group by ID2
              having min(ID1) <> max(ID1);


              I think that min() and max() have much better performance characteristics than count(distinct).



              In fact, I would expect this to work better than count(distinct):



              select id2
              from (select distinct id1, id2
              from t
              ) x
              group by id2
              having count(*) > 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',
                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%2f55065142%2fsql-group-by-using-two-keys%23new-answer', 'question_page');

                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                0














                You can use COUNT(DISTINCT ID1) in the having clause as the following:



                SELECT ID2
                FROM tbl
                GROUP BY sID2
                HAVING COUNT(DISTINCT ID1) > 1





                share|improve this answer



























                  0














                  You can use COUNT(DISTINCT ID1) in the having clause as the following:



                  SELECT ID2
                  FROM tbl
                  GROUP BY sID2
                  HAVING COUNT(DISTINCT ID1) > 1





                  share|improve this answer

























                    0












                    0








                    0







                    You can use COUNT(DISTINCT ID1) in the having clause as the following:



                    SELECT ID2
                    FROM tbl
                    GROUP BY sID2
                    HAVING COUNT(DISTINCT ID1) > 1





                    share|improve this answer













                    You can use COUNT(DISTINCT ID1) in the having clause as the following:



                    SELECT ID2
                    FROM tbl
                    GROUP BY sID2
                    HAVING COUNT(DISTINCT ID1) > 1






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 8 at 14:47









                    radrad

                    1,3961814




                    1,3961814























                        0














                        select ID2
                        from t
                        group by ID2
                        having count(distinct ID1) > 1


                        Not sure if Impala has count(distinct), but this is fairly standard so I am going to assume it. The having clause is applied after the group by, so it only keeps the data you are looking for.






                        share|improve this answer



























                          0














                          select ID2
                          from t
                          group by ID2
                          having count(distinct ID1) > 1


                          Not sure if Impala has count(distinct), but this is fairly standard so I am going to assume it. The having clause is applied after the group by, so it only keeps the data you are looking for.






                          share|improve this answer

























                            0












                            0








                            0







                            select ID2
                            from t
                            group by ID2
                            having count(distinct ID1) > 1


                            Not sure if Impala has count(distinct), but this is fairly standard so I am going to assume it. The having clause is applied after the group by, so it only keeps the data you are looking for.






                            share|improve this answer













                            select ID2
                            from t
                            group by ID2
                            having count(distinct ID1) > 1


                            Not sure if Impala has count(distinct), but this is fairly standard so I am going to assume it. The having clause is applied after the group by, so it only keeps the data you are looking for.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 8 at 14:48









                            GuillaumeGuillaume

                            1,53011227




                            1,53011227





















                                0














                                I would recommend:



                                select ID2
                                from t
                                group by ID2
                                having min(ID1) <> max(ID1);


                                I think that min() and max() have much better performance characteristics than count(distinct).



                                In fact, I would expect this to work better than count(distinct):



                                select id2
                                from (select distinct id1, id2
                                from t
                                ) x
                                group by id2
                                having count(*) > 1;





                                share|improve this answer



























                                  0














                                  I would recommend:



                                  select ID2
                                  from t
                                  group by ID2
                                  having min(ID1) <> max(ID1);


                                  I think that min() and max() have much better performance characteristics than count(distinct).



                                  In fact, I would expect this to work better than count(distinct):



                                  select id2
                                  from (select distinct id1, id2
                                  from t
                                  ) x
                                  group by id2
                                  having count(*) > 1;





                                  share|improve this answer

























                                    0












                                    0








                                    0







                                    I would recommend:



                                    select ID2
                                    from t
                                    group by ID2
                                    having min(ID1) <> max(ID1);


                                    I think that min() and max() have much better performance characteristics than count(distinct).



                                    In fact, I would expect this to work better than count(distinct):



                                    select id2
                                    from (select distinct id1, id2
                                    from t
                                    ) x
                                    group by id2
                                    having count(*) > 1;





                                    share|improve this answer













                                    I would recommend:



                                    select ID2
                                    from t
                                    group by ID2
                                    having min(ID1) <> max(ID1);


                                    I think that min() and max() have much better performance characteristics than count(distinct).



                                    In fact, I would expect this to work better than count(distinct):



                                    select id2
                                    from (select distinct id1, id2
                                    from t
                                    ) x
                                    group by id2
                                    having count(*) > 1;






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Mar 8 at 15:08









                                    Gordon LinoffGordon Linoff

                                    792k36316419




                                    792k36316419



























                                        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%2f55065142%2fsql-group-by-using-two-keys%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

                                        Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

                                        Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

                                        2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived