Find the row with the highest value in SQL











up vote
1
down vote

favorite












According to the schema, the Orders table has the following columns:



OrderId integer, CustomerId integer, RetailerId integer, ProductId integer, Count integer



I'm trying to figure out what product has the highest number of orders for each retailer.



So, to sum up all the orders per product, I have the following query:



SELECT RetailerId, ProductId, SUM(Count) AS ProductTotal
FROM Orders
GROUP BY RetailerId, ProductId
ORDER BY RetailerId, ProductTotal DESC;


This gives an output like this:



RETAILERID  PRODUCTID PRODUCTTOTAL
---------- ---------- ------------
1 5 115
1 10 45
1 1 15
1 4 2
1 8 1
2 9 12
2 11 10
2 7 1
3 3 3
4 2 1
5 11 1


Now, all I want to do is find the product with the highest number of orders per retailer. Right now, it shows all the products; I want only one.



I have tried way too many things. Following is one particularly abhorrent example:



SELECT O.RetailerId, O.ProductId, O.ProductTotal
FROM (
SELECT Orders.ProductId, MAX(ProductTotal) AS MaxProductTotal
FROM (
SELECT Orders.ProductId AS PID, SUM(Orders.Count) AS ProductTotal
FROM Orders
GROUP BY Orders.ProductId
) AS O INNER JOIN Orders ON Orders.ProductId = PID
GROUP BY Orders.ProductId
) AS X INNER JOIN O ON O.RetailerId = X.RetailerId AND O.ProductTotal = X.MaxProductTotal;


The solution to this is probably the simplest thing ever, but I just can't right now. So, I'd like some help.










share|improve this question




























    up vote
    1
    down vote

    favorite












    According to the schema, the Orders table has the following columns:



    OrderId integer, CustomerId integer, RetailerId integer, ProductId integer, Count integer



    I'm trying to figure out what product has the highest number of orders for each retailer.



    So, to sum up all the orders per product, I have the following query:



    SELECT RetailerId, ProductId, SUM(Count) AS ProductTotal
    FROM Orders
    GROUP BY RetailerId, ProductId
    ORDER BY RetailerId, ProductTotal DESC;


    This gives an output like this:



    RETAILERID  PRODUCTID PRODUCTTOTAL
    ---------- ---------- ------------
    1 5 115
    1 10 45
    1 1 15
    1 4 2
    1 8 1
    2 9 12
    2 11 10
    2 7 1
    3 3 3
    4 2 1
    5 11 1


    Now, all I want to do is find the product with the highest number of orders per retailer. Right now, it shows all the products; I want only one.



    I have tried way too many things. Following is one particularly abhorrent example:



    SELECT O.RetailerId, O.ProductId, O.ProductTotal
    FROM (
    SELECT Orders.ProductId, MAX(ProductTotal) AS MaxProductTotal
    FROM (
    SELECT Orders.ProductId AS PID, SUM(Orders.Count) AS ProductTotal
    FROM Orders
    GROUP BY Orders.ProductId
    ) AS O INNER JOIN Orders ON Orders.ProductId = PID
    GROUP BY Orders.ProductId
    ) AS X INNER JOIN O ON O.RetailerId = X.RetailerId AND O.ProductTotal = X.MaxProductTotal;


    The solution to this is probably the simplest thing ever, but I just can't right now. So, I'd like some help.










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      According to the schema, the Orders table has the following columns:



      OrderId integer, CustomerId integer, RetailerId integer, ProductId integer, Count integer



      I'm trying to figure out what product has the highest number of orders for each retailer.



      So, to sum up all the orders per product, I have the following query:



      SELECT RetailerId, ProductId, SUM(Count) AS ProductTotal
      FROM Orders
      GROUP BY RetailerId, ProductId
      ORDER BY RetailerId, ProductTotal DESC;


      This gives an output like this:



      RETAILERID  PRODUCTID PRODUCTTOTAL
      ---------- ---------- ------------
      1 5 115
      1 10 45
      1 1 15
      1 4 2
      1 8 1
      2 9 12
      2 11 10
      2 7 1
      3 3 3
      4 2 1
      5 11 1


      Now, all I want to do is find the product with the highest number of orders per retailer. Right now, it shows all the products; I want only one.



      I have tried way too many things. Following is one particularly abhorrent example:



      SELECT O.RetailerId, O.ProductId, O.ProductTotal
      FROM (
      SELECT Orders.ProductId, MAX(ProductTotal) AS MaxProductTotal
      FROM (
      SELECT Orders.ProductId AS PID, SUM(Orders.Count) AS ProductTotal
      FROM Orders
      GROUP BY Orders.ProductId
      ) AS O INNER JOIN Orders ON Orders.ProductId = PID
      GROUP BY Orders.ProductId
      ) AS X INNER JOIN O ON O.RetailerId = X.RetailerId AND O.ProductTotal = X.MaxProductTotal;


      The solution to this is probably the simplest thing ever, but I just can't right now. So, I'd like some help.










      share|improve this question















      According to the schema, the Orders table has the following columns:



      OrderId integer, CustomerId integer, RetailerId integer, ProductId integer, Count integer



      I'm trying to figure out what product has the highest number of orders for each retailer.



      So, to sum up all the orders per product, I have the following query:



      SELECT RetailerId, ProductId, SUM(Count) AS ProductTotal
      FROM Orders
      GROUP BY RetailerId, ProductId
      ORDER BY RetailerId, ProductTotal DESC;


      This gives an output like this:



      RETAILERID  PRODUCTID PRODUCTTOTAL
      ---------- ---------- ------------
      1 5 115
      1 10 45
      1 1 15
      1 4 2
      1 8 1
      2 9 12
      2 11 10
      2 7 1
      3 3 3
      4 2 1
      5 11 1


      Now, all I want to do is find the product with the highest number of orders per retailer. Right now, it shows all the products; I want only one.



      I have tried way too many things. Following is one particularly abhorrent example:



      SELECT O.RetailerId, O.ProductId, O.ProductTotal
      FROM (
      SELECT Orders.ProductId, MAX(ProductTotal) AS MaxProductTotal
      FROM (
      SELECT Orders.ProductId AS PID, SUM(Orders.Count) AS ProductTotal
      FROM Orders
      GROUP BY Orders.ProductId
      ) AS O INNER JOIN Orders ON Orders.ProductId = PID
      GROUP BY Orders.ProductId
      ) AS X INNER JOIN O ON O.RetailerId = X.RetailerId AND O.ProductTotal = X.MaxProductTotal;


      The solution to this is probably the simplest thing ever, but I just can't right now. So, I'd like some help.







      sql oracle greatest-n-per-group






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 14 hours ago









      a_horse_with_no_name

      284k45426524




      284k45426524










      asked 18 hours ago









      Sam Fischer

      9951327




      9951327
























          5 Answers
          5






          active

          oldest

          votes

















          up vote
          0
          down vote













          You can try using window function row_number()



          select * from
          (
          select *,row_number() over(partition by RetailerId order by ProductTotal desc) as rn from
          (
          SELECT RetailerId, ProductId, SUM(Count) AS ProductTotal
          FROM Orders
          GROUP BY RetailerId, ProductId
          )A
          )X where rn=1





          share|improve this answer




























            up vote
            0
            down vote













            you can use row_number() with cte



            with cte as
            (
            SELECT o.RetailerId,o.ProductId AS PID, SUM(o.Count) AS ProductTotal
            FROM Orders o
            GROUP BY o.ProductId,o.RetailerId
            ), cte2 as
            (
            select RetailerId,PID,ProductTotal,
            row_number() over(partition by RetailerId order by ProductTotal desc) as rn
            from cte
            ) select RetailerId,PID,ProductTotal from cte2 where rn=1





            share|improve this answer




























              up vote
              0
              down vote













              Have you tried using rank?



              Try this query



              select OrderC, retailer_id,product_id from ( select sum(count_order) as OrderC,retailer_id,product_id,row_number() over(order by OrderC desc) as RN from order_t  group  by retailer_id,product_id) as d where RN=1;





              share|improve this answer








              New contributor




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

























                up vote
                0
                down vote













                Here is a version without sub-query using FIRST/LAST:



                WITH t(RETAILERID, PRODUCTID, PRODUCTTOTAL) AS (    
                SELECT 1, 5, 115 FROM dual UNION ALL
                SELECT 1, 10, 45 FROM dual UNION ALL
                SELECT 1, 1, 15 FROM dual UNION ALL
                SELECT 1, 4, 2 FROM dual UNION ALL
                SELECT 1, 8, 1 FROM dual UNION ALL
                SELECT 2, 9, 12 FROM dual UNION ALL
                SELECT 2, 11, 10 FROM dual UNION ALL
                SELECT 2, 7, 1 FROM dual UNION ALL
                SELECT 3, 3, 3 FROM dual UNION ALL
                SELECT 4, 2, 1 FROM dual UNION ALL
                SELECT 5,11, 1 FROM dual)
                SELECT
                RETAILERID,
                MAX(PRODUCTID) KEEP (DENSE_RANK LAST ORDER BY PRODUCTTOTAL) AS PRODUCTID,
                MAX(PRODUCTTOTAL)
                FROM t
                GROUP BY RETAILERID;


                +--------------------------------------+
                |RETAILERID|PRODUCTID|MAX(PRODUCTTOTAL)|
                +--------------------------------------+
                |1 |5 |115 |
                |2 |9 |12 |
                |3 |3 |3 |
                |4 |2 |1 |
                |5 |11 |1 |
                +--------------------------------------+





                share|improve this answer




























                  up vote
                  0
                  down vote













                  Select the maximum total per customer with a window function:



                  SELECT RetailerId, ProductId, ProductTotal
                  FROM
                  (
                  SELECT
                  RetailerId, ProductId, SUM(Count) AS ProductTotal,
                  MAX(SUM(Count)) OVER (PARTITION BY RetailerId) AS MaxProductTotal
                  FROM Orders
                  GROUP BY RetailerId, ProductId
                  )
                  WHERE ProductTotal = MaxProductTotal
                  ORDER BY RetailerId;





                  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: 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%2f53201588%2ffind-the-row-with-the-highest-value-in-sql%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest
































                    5 Answers
                    5






                    active

                    oldest

                    votes








                    5 Answers
                    5






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes








                    up vote
                    0
                    down vote













                    You can try using window function row_number()



                    select * from
                    (
                    select *,row_number() over(partition by RetailerId order by ProductTotal desc) as rn from
                    (
                    SELECT RetailerId, ProductId, SUM(Count) AS ProductTotal
                    FROM Orders
                    GROUP BY RetailerId, ProductId
                    )A
                    )X where rn=1





                    share|improve this answer

























                      up vote
                      0
                      down vote













                      You can try using window function row_number()



                      select * from
                      (
                      select *,row_number() over(partition by RetailerId order by ProductTotal desc) as rn from
                      (
                      SELECT RetailerId, ProductId, SUM(Count) AS ProductTotal
                      FROM Orders
                      GROUP BY RetailerId, ProductId
                      )A
                      )X where rn=1





                      share|improve this answer























                        up vote
                        0
                        down vote










                        up vote
                        0
                        down vote









                        You can try using window function row_number()



                        select * from
                        (
                        select *,row_number() over(partition by RetailerId order by ProductTotal desc) as rn from
                        (
                        SELECT RetailerId, ProductId, SUM(Count) AS ProductTotal
                        FROM Orders
                        GROUP BY RetailerId, ProductId
                        )A
                        )X where rn=1





                        share|improve this answer












                        You can try using window function row_number()



                        select * from
                        (
                        select *,row_number() over(partition by RetailerId order by ProductTotal desc) as rn from
                        (
                        SELECT RetailerId, ProductId, SUM(Count) AS ProductTotal
                        FROM Orders
                        GROUP BY RetailerId, ProductId
                        )A
                        )X where rn=1






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered 17 hours ago









                        fa06

                        7,7601515




                        7,7601515
























                            up vote
                            0
                            down vote













                            you can use row_number() with cte



                            with cte as
                            (
                            SELECT o.RetailerId,o.ProductId AS PID, SUM(o.Count) AS ProductTotal
                            FROM Orders o
                            GROUP BY o.ProductId,o.RetailerId
                            ), cte2 as
                            (
                            select RetailerId,PID,ProductTotal,
                            row_number() over(partition by RetailerId order by ProductTotal desc) as rn
                            from cte
                            ) select RetailerId,PID,ProductTotal from cte2 where rn=1





                            share|improve this answer

























                              up vote
                              0
                              down vote













                              you can use row_number() with cte



                              with cte as
                              (
                              SELECT o.RetailerId,o.ProductId AS PID, SUM(o.Count) AS ProductTotal
                              FROM Orders o
                              GROUP BY o.ProductId,o.RetailerId
                              ), cte2 as
                              (
                              select RetailerId,PID,ProductTotal,
                              row_number() over(partition by RetailerId order by ProductTotal desc) as rn
                              from cte
                              ) select RetailerId,PID,ProductTotal from cte2 where rn=1





                              share|improve this answer























                                up vote
                                0
                                down vote










                                up vote
                                0
                                down vote









                                you can use row_number() with cte



                                with cte as
                                (
                                SELECT o.RetailerId,o.ProductId AS PID, SUM(o.Count) AS ProductTotal
                                FROM Orders o
                                GROUP BY o.ProductId,o.RetailerId
                                ), cte2 as
                                (
                                select RetailerId,PID,ProductTotal,
                                row_number() over(partition by RetailerId order by ProductTotal desc) as rn
                                from cte
                                ) select RetailerId,PID,ProductTotal from cte2 where rn=1





                                share|improve this answer












                                you can use row_number() with cte



                                with cte as
                                (
                                SELECT o.RetailerId,o.ProductId AS PID, SUM(o.Count) AS ProductTotal
                                FROM Orders o
                                GROUP BY o.ProductId,o.RetailerId
                                ), cte2 as
                                (
                                select RetailerId,PID,ProductTotal,
                                row_number() over(partition by RetailerId order by ProductTotal desc) as rn
                                from cte
                                ) select RetailerId,PID,ProductTotal from cte2 where rn=1






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered 17 hours ago









                                Zaynul Abadin Tuhin

                                9,8502730




                                9,8502730






















                                    up vote
                                    0
                                    down vote













                                    Have you tried using rank?



                                    Try this query



                                    select OrderC, retailer_id,product_id from ( select sum(count_order) as OrderC,retailer_id,product_id,row_number() over(order by OrderC desc) as RN from order_t  group  by retailer_id,product_id) as d where RN=1;





                                    share|improve this answer








                                    New contributor




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






















                                      up vote
                                      0
                                      down vote













                                      Have you tried using rank?



                                      Try this query



                                      select OrderC, retailer_id,product_id from ( select sum(count_order) as OrderC,retailer_id,product_id,row_number() over(order by OrderC desc) as RN from order_t  group  by retailer_id,product_id) as d where RN=1;





                                      share|improve this answer








                                      New contributor




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




















                                        up vote
                                        0
                                        down vote










                                        up vote
                                        0
                                        down vote









                                        Have you tried using rank?



                                        Try this query



                                        select OrderC, retailer_id,product_id from ( select sum(count_order) as OrderC,retailer_id,product_id,row_number() over(order by OrderC desc) as RN from order_t  group  by retailer_id,product_id) as d where RN=1;





                                        share|improve this answer








                                        New contributor




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









                                        Have you tried using rank?



                                        Try this query



                                        select OrderC, retailer_id,product_id from ( select sum(count_order) as OrderC,retailer_id,product_id,row_number() over(order by OrderC desc) as RN from order_t  group  by retailer_id,product_id) as d where RN=1;






                                        share|improve this answer








                                        New contributor




                                        Brekhnaa 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 answer



                                        share|improve this answer






                                        New contributor




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









                                        answered 16 hours ago









                                        Brekhnaa

                                        162




                                        162




                                        New contributor




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





                                        New contributor





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






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






















                                            up vote
                                            0
                                            down vote













                                            Here is a version without sub-query using FIRST/LAST:



                                            WITH t(RETAILERID, PRODUCTID, PRODUCTTOTAL) AS (    
                                            SELECT 1, 5, 115 FROM dual UNION ALL
                                            SELECT 1, 10, 45 FROM dual UNION ALL
                                            SELECT 1, 1, 15 FROM dual UNION ALL
                                            SELECT 1, 4, 2 FROM dual UNION ALL
                                            SELECT 1, 8, 1 FROM dual UNION ALL
                                            SELECT 2, 9, 12 FROM dual UNION ALL
                                            SELECT 2, 11, 10 FROM dual UNION ALL
                                            SELECT 2, 7, 1 FROM dual UNION ALL
                                            SELECT 3, 3, 3 FROM dual UNION ALL
                                            SELECT 4, 2, 1 FROM dual UNION ALL
                                            SELECT 5,11, 1 FROM dual)
                                            SELECT
                                            RETAILERID,
                                            MAX(PRODUCTID) KEEP (DENSE_RANK LAST ORDER BY PRODUCTTOTAL) AS PRODUCTID,
                                            MAX(PRODUCTTOTAL)
                                            FROM t
                                            GROUP BY RETAILERID;


                                            +--------------------------------------+
                                            |RETAILERID|PRODUCTID|MAX(PRODUCTTOTAL)|
                                            +--------------------------------------+
                                            |1 |5 |115 |
                                            |2 |9 |12 |
                                            |3 |3 |3 |
                                            |4 |2 |1 |
                                            |5 |11 |1 |
                                            +--------------------------------------+





                                            share|improve this answer

























                                              up vote
                                              0
                                              down vote













                                              Here is a version without sub-query using FIRST/LAST:



                                              WITH t(RETAILERID, PRODUCTID, PRODUCTTOTAL) AS (    
                                              SELECT 1, 5, 115 FROM dual UNION ALL
                                              SELECT 1, 10, 45 FROM dual UNION ALL
                                              SELECT 1, 1, 15 FROM dual UNION ALL
                                              SELECT 1, 4, 2 FROM dual UNION ALL
                                              SELECT 1, 8, 1 FROM dual UNION ALL
                                              SELECT 2, 9, 12 FROM dual UNION ALL
                                              SELECT 2, 11, 10 FROM dual UNION ALL
                                              SELECT 2, 7, 1 FROM dual UNION ALL
                                              SELECT 3, 3, 3 FROM dual UNION ALL
                                              SELECT 4, 2, 1 FROM dual UNION ALL
                                              SELECT 5,11, 1 FROM dual)
                                              SELECT
                                              RETAILERID,
                                              MAX(PRODUCTID) KEEP (DENSE_RANK LAST ORDER BY PRODUCTTOTAL) AS PRODUCTID,
                                              MAX(PRODUCTTOTAL)
                                              FROM t
                                              GROUP BY RETAILERID;


                                              +--------------------------------------+
                                              |RETAILERID|PRODUCTID|MAX(PRODUCTTOTAL)|
                                              +--------------------------------------+
                                              |1 |5 |115 |
                                              |2 |9 |12 |
                                              |3 |3 |3 |
                                              |4 |2 |1 |
                                              |5 |11 |1 |
                                              +--------------------------------------+





                                              share|improve this answer























                                                up vote
                                                0
                                                down vote










                                                up vote
                                                0
                                                down vote









                                                Here is a version without sub-query using FIRST/LAST:



                                                WITH t(RETAILERID, PRODUCTID, PRODUCTTOTAL) AS (    
                                                SELECT 1, 5, 115 FROM dual UNION ALL
                                                SELECT 1, 10, 45 FROM dual UNION ALL
                                                SELECT 1, 1, 15 FROM dual UNION ALL
                                                SELECT 1, 4, 2 FROM dual UNION ALL
                                                SELECT 1, 8, 1 FROM dual UNION ALL
                                                SELECT 2, 9, 12 FROM dual UNION ALL
                                                SELECT 2, 11, 10 FROM dual UNION ALL
                                                SELECT 2, 7, 1 FROM dual UNION ALL
                                                SELECT 3, 3, 3 FROM dual UNION ALL
                                                SELECT 4, 2, 1 FROM dual UNION ALL
                                                SELECT 5,11, 1 FROM dual)
                                                SELECT
                                                RETAILERID,
                                                MAX(PRODUCTID) KEEP (DENSE_RANK LAST ORDER BY PRODUCTTOTAL) AS PRODUCTID,
                                                MAX(PRODUCTTOTAL)
                                                FROM t
                                                GROUP BY RETAILERID;


                                                +--------------------------------------+
                                                |RETAILERID|PRODUCTID|MAX(PRODUCTTOTAL)|
                                                +--------------------------------------+
                                                |1 |5 |115 |
                                                |2 |9 |12 |
                                                |3 |3 |3 |
                                                |4 |2 |1 |
                                                |5 |11 |1 |
                                                +--------------------------------------+





                                                share|improve this answer












                                                Here is a version without sub-query using FIRST/LAST:



                                                WITH t(RETAILERID, PRODUCTID, PRODUCTTOTAL) AS (    
                                                SELECT 1, 5, 115 FROM dual UNION ALL
                                                SELECT 1, 10, 45 FROM dual UNION ALL
                                                SELECT 1, 1, 15 FROM dual UNION ALL
                                                SELECT 1, 4, 2 FROM dual UNION ALL
                                                SELECT 1, 8, 1 FROM dual UNION ALL
                                                SELECT 2, 9, 12 FROM dual UNION ALL
                                                SELECT 2, 11, 10 FROM dual UNION ALL
                                                SELECT 2, 7, 1 FROM dual UNION ALL
                                                SELECT 3, 3, 3 FROM dual UNION ALL
                                                SELECT 4, 2, 1 FROM dual UNION ALL
                                                SELECT 5,11, 1 FROM dual)
                                                SELECT
                                                RETAILERID,
                                                MAX(PRODUCTID) KEEP (DENSE_RANK LAST ORDER BY PRODUCTTOTAL) AS PRODUCTID,
                                                MAX(PRODUCTTOTAL)
                                                FROM t
                                                GROUP BY RETAILERID;


                                                +--------------------------------------+
                                                |RETAILERID|PRODUCTID|MAX(PRODUCTTOTAL)|
                                                +--------------------------------------+
                                                |1 |5 |115 |
                                                |2 |9 |12 |
                                                |3 |3 |3 |
                                                |4 |2 |1 |
                                                |5 |11 |1 |
                                                +--------------------------------------+






                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered 15 hours ago









                                                Wernfried Domscheit

                                                22.9k42757




                                                22.9k42757






















                                                    up vote
                                                    0
                                                    down vote













                                                    Select the maximum total per customer with a window function:



                                                    SELECT RetailerId, ProductId, ProductTotal
                                                    FROM
                                                    (
                                                    SELECT
                                                    RetailerId, ProductId, SUM(Count) AS ProductTotal,
                                                    MAX(SUM(Count)) OVER (PARTITION BY RetailerId) AS MaxProductTotal
                                                    FROM Orders
                                                    GROUP BY RetailerId, ProductId
                                                    )
                                                    WHERE ProductTotal = MaxProductTotal
                                                    ORDER BY RetailerId;





                                                    share|improve this answer

























                                                      up vote
                                                      0
                                                      down vote













                                                      Select the maximum total per customer with a window function:



                                                      SELECT RetailerId, ProductId, ProductTotal
                                                      FROM
                                                      (
                                                      SELECT
                                                      RetailerId, ProductId, SUM(Count) AS ProductTotal,
                                                      MAX(SUM(Count)) OVER (PARTITION BY RetailerId) AS MaxProductTotal
                                                      FROM Orders
                                                      GROUP BY RetailerId, ProductId
                                                      )
                                                      WHERE ProductTotal = MaxProductTotal
                                                      ORDER BY RetailerId;





                                                      share|improve this answer























                                                        up vote
                                                        0
                                                        down vote










                                                        up vote
                                                        0
                                                        down vote









                                                        Select the maximum total per customer with a window function:



                                                        SELECT RetailerId, ProductId, ProductTotal
                                                        FROM
                                                        (
                                                        SELECT
                                                        RetailerId, ProductId, SUM(Count) AS ProductTotal,
                                                        MAX(SUM(Count)) OVER (PARTITION BY RetailerId) AS MaxProductTotal
                                                        FROM Orders
                                                        GROUP BY RetailerId, ProductId
                                                        )
                                                        WHERE ProductTotal = MaxProductTotal
                                                        ORDER BY RetailerId;





                                                        share|improve this answer












                                                        Select the maximum total per customer with a window function:



                                                        SELECT RetailerId, ProductId, ProductTotal
                                                        FROM
                                                        (
                                                        SELECT
                                                        RetailerId, ProductId, SUM(Count) AS ProductTotal,
                                                        MAX(SUM(Count)) OVER (PARTITION BY RetailerId) AS MaxProductTotal
                                                        FROM Orders
                                                        GROUP BY RetailerId, ProductId
                                                        )
                                                        WHERE ProductTotal = MaxProductTotal
                                                        ORDER BY RetailerId;






                                                        share|improve this answer












                                                        share|improve this answer



                                                        share|improve this answer










                                                        answered 14 hours ago









                                                        Thorsten Kettner

                                                        49.6k22441




                                                        49.6k22441






























                                                             

                                                            draft saved


                                                            draft discarded



















































                                                             


                                                            draft saved


                                                            draft discarded














                                                            StackExchange.ready(
                                                            function () {
                                                            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53201588%2ffind-the-row-with-the-highest-value-in-sql%23new-answer', 'question_page');
                                                            }
                                                            );

                                                            Post as a guest




















































































                                                            Popular posts from this blog

                                                            Schultheiß

                                                            Verwaltungsgliederung Dänemarks

                                                            Liste der Kulturdenkmale in Wilsdruff