Fetch 2 records for each group name of element of column 1 that in column 2 have the most value










1















I have a table that like below. Now i want to fetch 2 records for each group name of element of column 1 that in column 2 have the most value. For example, Fetch 85 and 75 for A, 65 and 45 for B ...
I use oracle database.
TNX



 ----------------------
|Column 1 | Column 2 |
----------------------
| A | 85 |
----------------------
| A | 75 |
---------------------
| A | 60 |
---------------------
| A | 50 |
---------------------
| B | 65 |
---------------------
| B | 45 |
---------------------
| B | 35 |
---------------------
| B | 25 |
---------------------









share|improve this question




























    1















    I have a table that like below. Now i want to fetch 2 records for each group name of element of column 1 that in column 2 have the most value. For example, Fetch 85 and 75 for A, 65 and 45 for B ...
    I use oracle database.
    TNX



     ----------------------
    |Column 1 | Column 2 |
    ----------------------
    | A | 85 |
    ----------------------
    | A | 75 |
    ---------------------
    | A | 60 |
    ---------------------
    | A | 50 |
    ---------------------
    | B | 65 |
    ---------------------
    | B | 45 |
    ---------------------
    | B | 35 |
    ---------------------
    | B | 25 |
    ---------------------









    share|improve this question


























      1












      1








      1








      I have a table that like below. Now i want to fetch 2 records for each group name of element of column 1 that in column 2 have the most value. For example, Fetch 85 and 75 for A, 65 and 45 for B ...
      I use oracle database.
      TNX



       ----------------------
      |Column 1 | Column 2 |
      ----------------------
      | A | 85 |
      ----------------------
      | A | 75 |
      ---------------------
      | A | 60 |
      ---------------------
      | A | 50 |
      ---------------------
      | B | 65 |
      ---------------------
      | B | 45 |
      ---------------------
      | B | 35 |
      ---------------------
      | B | 25 |
      ---------------------









      share|improve this question
















      I have a table that like below. Now i want to fetch 2 records for each group name of element of column 1 that in column 2 have the most value. For example, Fetch 85 and 75 for A, 65 and 45 for B ...
      I use oracle database.
      TNX



       ----------------------
      |Column 1 | Column 2 |
      ----------------------
      | A | 85 |
      ----------------------
      | A | 75 |
      ---------------------
      | A | 60 |
      ---------------------
      | A | 50 |
      ---------------------
      | B | 65 |
      ---------------------
      | B | 45 |
      ---------------------
      | B | 35 |
      ---------------------
      | B | 25 |
      ---------------------






      sql oracle






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 9:46







      Yaser

















      asked Nov 13 '18 at 9:43









      YaserYaser

      154




      154






















          4 Answers
          4






          active

          oldest

          votes


















          0














          You can use row_number() :



          select t.*
          from (select t.*,
          row_number() over (partition by col1 order by col2 desc) as seq
          from table t
          ) t
          where seq <= 2;


          However, fetch first . . . clause also helpful :



          select t.*
          from table t
          where t.col2 in (select t1.col2
          from table t1
          where t1.col1 = t.col1
          order by t1.col2 desc
          fetch first 2 rows only
          );





          share|improve this answer

























          • TNX. Your first query is ok, but second query is wrong. The resault of second query is not equal by another.

            – Yaser
            Nov 13 '18 at 12:11



















          0














          use row_number window function



          with t1 as
          (
          select col1,col2
          ,row_number() over(partition by col1 order by col2 desc) rn
          from table_name
          ) select * from t1 where rn<=2





          share|improve this answer






























            0














            Try using row_number()



            select * from
            (
            select *, row_number() over(partition by col1 order by col2 desc) as rn
            from tablename
            ) A where rn in (1,2)





            share|improve this answer
































              0














              Try this



              Here is demo solution



              SQL



              select col1,col2 from (select col1,col2, rank() over(partition by col1 order by col2 desc) as rn from t) q where q.rn<=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%2f53278064%2ffetch-2-records-for-each-group-name-of-element-of-column-1-that-in-column-2-have%23new-answer', 'question_page');

                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                0














                You can use row_number() :



                select t.*
                from (select t.*,
                row_number() over (partition by col1 order by col2 desc) as seq
                from table t
                ) t
                where seq <= 2;


                However, fetch first . . . clause also helpful :



                select t.*
                from table t
                where t.col2 in (select t1.col2
                from table t1
                where t1.col1 = t.col1
                order by t1.col2 desc
                fetch first 2 rows only
                );





                share|improve this answer

























                • TNX. Your first query is ok, but second query is wrong. The resault of second query is not equal by another.

                  – Yaser
                  Nov 13 '18 at 12:11
















                0














                You can use row_number() :



                select t.*
                from (select t.*,
                row_number() over (partition by col1 order by col2 desc) as seq
                from table t
                ) t
                where seq <= 2;


                However, fetch first . . . clause also helpful :



                select t.*
                from table t
                where t.col2 in (select t1.col2
                from table t1
                where t1.col1 = t.col1
                order by t1.col2 desc
                fetch first 2 rows only
                );





                share|improve this answer

























                • TNX. Your first query is ok, but second query is wrong. The resault of second query is not equal by another.

                  – Yaser
                  Nov 13 '18 at 12:11














                0












                0








                0







                You can use row_number() :



                select t.*
                from (select t.*,
                row_number() over (partition by col1 order by col2 desc) as seq
                from table t
                ) t
                where seq <= 2;


                However, fetch first . . . clause also helpful :



                select t.*
                from table t
                where t.col2 in (select t1.col2
                from table t1
                where t1.col1 = t.col1
                order by t1.col2 desc
                fetch first 2 rows only
                );





                share|improve this answer















                You can use row_number() :



                select t.*
                from (select t.*,
                row_number() over (partition by col1 order by col2 desc) as seq
                from table t
                ) t
                where seq <= 2;


                However, fetch first . . . clause also helpful :



                select t.*
                from table t
                where t.col2 in (select t1.col2
                from table t1
                where t1.col1 = t.col1
                order by t1.col2 desc
                fetch first 2 rows only
                );






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 13 '18 at 12:12

























                answered Nov 13 '18 at 9:45









                Yogesh SharmaYogesh Sharma

                30.6k51437




                30.6k51437












                • TNX. Your first query is ok, but second query is wrong. The resault of second query is not equal by another.

                  – Yaser
                  Nov 13 '18 at 12:11


















                • TNX. Your first query is ok, but second query is wrong. The resault of second query is not equal by another.

                  – Yaser
                  Nov 13 '18 at 12:11

















                TNX. Your first query is ok, but second query is wrong. The resault of second query is not equal by another.

                – Yaser
                Nov 13 '18 at 12:11






                TNX. Your first query is ok, but second query is wrong. The resault of second query is not equal by another.

                – Yaser
                Nov 13 '18 at 12:11














                0














                use row_number window function



                with t1 as
                (
                select col1,col2
                ,row_number() over(partition by col1 order by col2 desc) rn
                from table_name
                ) select * from t1 where rn<=2





                share|improve this answer



























                  0














                  use row_number window function



                  with t1 as
                  (
                  select col1,col2
                  ,row_number() over(partition by col1 order by col2 desc) rn
                  from table_name
                  ) select * from t1 where rn<=2





                  share|improve this answer

























                    0












                    0








                    0







                    use row_number window function



                    with t1 as
                    (
                    select col1,col2
                    ,row_number() over(partition by col1 order by col2 desc) rn
                    from table_name
                    ) select * from t1 where rn<=2





                    share|improve this answer













                    use row_number window function



                    with t1 as
                    (
                    select col1,col2
                    ,row_number() over(partition by col1 order by col2 desc) rn
                    from table_name
                    ) select * from t1 where rn<=2






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 13 '18 at 9:45









                    Zaynul Abadin TuhinZaynul Abadin Tuhin

                    13k21032




                    13k21032





















                        0














                        Try using row_number()



                        select * from
                        (
                        select *, row_number() over(partition by col1 order by col2 desc) as rn
                        from tablename
                        ) A where rn in (1,2)





                        share|improve this answer





























                          0














                          Try using row_number()



                          select * from
                          (
                          select *, row_number() over(partition by col1 order by col2 desc) as rn
                          from tablename
                          ) A where rn in (1,2)





                          share|improve this answer



























                            0












                            0








                            0







                            Try using row_number()



                            select * from
                            (
                            select *, row_number() over(partition by col1 order by col2 desc) as rn
                            from tablename
                            ) A where rn in (1,2)





                            share|improve this answer















                            Try using row_number()



                            select * from
                            (
                            select *, row_number() over(partition by col1 order by col2 desc) as rn
                            from tablename
                            ) A where rn in (1,2)






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 13 '18 at 9:53

























                            answered Nov 13 '18 at 9:45









                            fa06fa06

                            13.1k2917




                            13.1k2917





















                                0














                                Try this



                                Here is demo solution



                                SQL



                                select col1,col2 from (select col1,col2, rank() over(partition by col1 order by col2 desc) as rn from t) q where q.rn<=2;





                                share|improve this answer



























                                  0














                                  Try this



                                  Here is demo solution



                                  SQL



                                  select col1,col2 from (select col1,col2, rank() over(partition by col1 order by col2 desc) as rn from t) q where q.rn<=2;





                                  share|improve this answer

























                                    0












                                    0








                                    0







                                    Try this



                                    Here is demo solution



                                    SQL



                                    select col1,col2 from (select col1,col2, rank() over(partition by col1 order by col2 desc) as rn from t) q where q.rn<=2;





                                    share|improve this answer













                                    Try this



                                    Here is demo solution



                                    SQL



                                    select col1,col2 from (select col1,col2, rank() over(partition by col1 order by col2 desc) as rn from t) q where q.rn<=2;






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 13 '18 at 10:26









                                    BrekhnaaBrekhnaa

                                    363




                                    363



























                                        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%2f53278064%2ffetch-2-records-for-each-group-name-of-element-of-column-1-that-in-column-2-have%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

                                        Use pre created SQLite database for Android project in kotlin

                                        Darth Vader #20

                                        Ondo