Selecting Row(s) Based Upon Derived Value Maximum











up vote
1
down vote

favorite












I need to display the most followed individuals in a group of people.



SELECT * FROM User
JOIN(
SELECT DISTINCT f.followee_id, COUNT(*) as cnt
FROM Follow f
GROUP BY f.followee_id
ORDER BY cnt desc) derv_table
WHERE User.id = derv_table.followee_id


Results in this table



  id  |             email             |  zipcode   | followee_id | cnt 
-----|-------------------------------|------------|-------------|-----
80 | kkiehn@example.com | 81629-3826 | 80 | 2
39 | berenice.predovic@example.com | 90222-0327 | 39 | 2
4 | schaden.lea@example.com | 35465-6959 | 4 | 2
100 | kathryne.braun@example.org | 80558-1775 | 100 | 2
11 | auer.sterling@example.net | 06562-5156 | 11 | 1
49 | arlie.ortiz@example.org | 69874-3485 | 49 | 1
78 | beahan.andreanne@example.net | 73719-7076 | 78 | 1
13 | kaitlyn28@example.com | 16426-2360 | 13 | 1


So I've gotten as far as ordering which people have the most followers, since followee_id and id are the same key.



This table continues on, the CNT is a derived (or calculated value), how do I only display the rows which contains the maximum of CNT (for N records that have the maximum, so LIMIT will not suffice) I've tried all sorts of joins and different conditions and haven't gotten anywhere.










share|improve this question


























    up vote
    1
    down vote

    favorite












    I need to display the most followed individuals in a group of people.



    SELECT * FROM User
    JOIN(
    SELECT DISTINCT f.followee_id, COUNT(*) as cnt
    FROM Follow f
    GROUP BY f.followee_id
    ORDER BY cnt desc) derv_table
    WHERE User.id = derv_table.followee_id


    Results in this table



      id  |             email             |  zipcode   | followee_id | cnt 
    -----|-------------------------------|------------|-------------|-----
    80 | kkiehn@example.com | 81629-3826 | 80 | 2
    39 | berenice.predovic@example.com | 90222-0327 | 39 | 2
    4 | schaden.lea@example.com | 35465-6959 | 4 | 2
    100 | kathryne.braun@example.org | 80558-1775 | 100 | 2
    11 | auer.sterling@example.net | 06562-5156 | 11 | 1
    49 | arlie.ortiz@example.org | 69874-3485 | 49 | 1
    78 | beahan.andreanne@example.net | 73719-7076 | 78 | 1
    13 | kaitlyn28@example.com | 16426-2360 | 13 | 1


    So I've gotten as far as ordering which people have the most followers, since followee_id and id are the same key.



    This table continues on, the CNT is a derived (or calculated value), how do I only display the rows which contains the maximum of CNT (for N records that have the maximum, so LIMIT will not suffice) I've tried all sorts of joins and different conditions and haven't gotten anywhere.










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I need to display the most followed individuals in a group of people.



      SELECT * FROM User
      JOIN(
      SELECT DISTINCT f.followee_id, COUNT(*) as cnt
      FROM Follow f
      GROUP BY f.followee_id
      ORDER BY cnt desc) derv_table
      WHERE User.id = derv_table.followee_id


      Results in this table



        id  |             email             |  zipcode   | followee_id | cnt 
      -----|-------------------------------|------------|-------------|-----
      80 | kkiehn@example.com | 81629-3826 | 80 | 2
      39 | berenice.predovic@example.com | 90222-0327 | 39 | 2
      4 | schaden.lea@example.com | 35465-6959 | 4 | 2
      100 | kathryne.braun@example.org | 80558-1775 | 100 | 2
      11 | auer.sterling@example.net | 06562-5156 | 11 | 1
      49 | arlie.ortiz@example.org | 69874-3485 | 49 | 1
      78 | beahan.andreanne@example.net | 73719-7076 | 78 | 1
      13 | kaitlyn28@example.com | 16426-2360 | 13 | 1


      So I've gotten as far as ordering which people have the most followers, since followee_id and id are the same key.



      This table continues on, the CNT is a derived (or calculated value), how do I only display the rows which contains the maximum of CNT (for N records that have the maximum, so LIMIT will not suffice) I've tried all sorts of joins and different conditions and haven't gotten anywhere.










      share|improve this question













      I need to display the most followed individuals in a group of people.



      SELECT * FROM User
      JOIN(
      SELECT DISTINCT f.followee_id, COUNT(*) as cnt
      FROM Follow f
      GROUP BY f.followee_id
      ORDER BY cnt desc) derv_table
      WHERE User.id = derv_table.followee_id


      Results in this table



        id  |             email             |  zipcode   | followee_id | cnt 
      -----|-------------------------------|------------|-------------|-----
      80 | kkiehn@example.com | 81629-3826 | 80 | 2
      39 | berenice.predovic@example.com | 90222-0327 | 39 | 2
      4 | schaden.lea@example.com | 35465-6959 | 4 | 2
      100 | kathryne.braun@example.org | 80558-1775 | 100 | 2
      11 | auer.sterling@example.net | 06562-5156 | 11 | 1
      49 | arlie.ortiz@example.org | 69874-3485 | 49 | 1
      78 | beahan.andreanne@example.net | 73719-7076 | 78 | 1
      13 | kaitlyn28@example.com | 16426-2360 | 13 | 1


      So I've gotten as far as ordering which people have the most followers, since followee_id and id are the same key.



      This table continues on, the CNT is a derived (or calculated value), how do I only display the rows which contains the maximum of CNT (for N records that have the maximum, so LIMIT will not suffice) I've tried all sorts of joins and different conditions and haven't gotten anywhere.







      mysql derived-column






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 9 at 3:40









      Lost Modernity

      82




      82
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          In MySQL versions earlier than 8+, we can use a subquery:



          SELECT * FROM User u
          INNER JOIN
          (
          SELECT f.followee_id, COUNT(*) AS cnt
          FROM Follow f
          GROUP BY f.followee_id
          ) t
          ON u.id = t.followee_id
          WHERE
          t.cnt = (SELECT COUNT(*) FROM Follow
          GROUP BY followee_id ORDER BY COUNT(*) DESC LIMIT 1);


          In MySQL 8+ or later, we can take advantage of the RANK analytic function:



          WITH cte AS (
          SELECT *,
          RANK() OVER (ORDER BY t.cnt DESC) rnk
          FROM User u
          INNER JOIN
          (
          SELECT f.followee_id, COUNT(*) AS cnt
          FROM Follow f
          GROUP BY f.followee_id
          ) t
          ON u.id = t.followee_id
          )

          SELECT *
          FROM cte
          WHERE rnk = 1;





          share|improve this answer























          • Cheers dude, excellent.
            – Lost Modernity
            Nov 9 at 3:51










          • Time-limit :(, will do when possible.
            – Lost Modernity
            Nov 9 at 3:53











          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%2f53219528%2fselecting-rows-based-upon-derived-value-maximum%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          In MySQL versions earlier than 8+, we can use a subquery:



          SELECT * FROM User u
          INNER JOIN
          (
          SELECT f.followee_id, COUNT(*) AS cnt
          FROM Follow f
          GROUP BY f.followee_id
          ) t
          ON u.id = t.followee_id
          WHERE
          t.cnt = (SELECT COUNT(*) FROM Follow
          GROUP BY followee_id ORDER BY COUNT(*) DESC LIMIT 1);


          In MySQL 8+ or later, we can take advantage of the RANK analytic function:



          WITH cte AS (
          SELECT *,
          RANK() OVER (ORDER BY t.cnt DESC) rnk
          FROM User u
          INNER JOIN
          (
          SELECT f.followee_id, COUNT(*) AS cnt
          FROM Follow f
          GROUP BY f.followee_id
          ) t
          ON u.id = t.followee_id
          )

          SELECT *
          FROM cte
          WHERE rnk = 1;





          share|improve this answer























          • Cheers dude, excellent.
            – Lost Modernity
            Nov 9 at 3:51










          • Time-limit :(, will do when possible.
            – Lost Modernity
            Nov 9 at 3:53















          up vote
          0
          down vote



          accepted










          In MySQL versions earlier than 8+, we can use a subquery:



          SELECT * FROM User u
          INNER JOIN
          (
          SELECT f.followee_id, COUNT(*) AS cnt
          FROM Follow f
          GROUP BY f.followee_id
          ) t
          ON u.id = t.followee_id
          WHERE
          t.cnt = (SELECT COUNT(*) FROM Follow
          GROUP BY followee_id ORDER BY COUNT(*) DESC LIMIT 1);


          In MySQL 8+ or later, we can take advantage of the RANK analytic function:



          WITH cte AS (
          SELECT *,
          RANK() OVER (ORDER BY t.cnt DESC) rnk
          FROM User u
          INNER JOIN
          (
          SELECT f.followee_id, COUNT(*) AS cnt
          FROM Follow f
          GROUP BY f.followee_id
          ) t
          ON u.id = t.followee_id
          )

          SELECT *
          FROM cte
          WHERE rnk = 1;





          share|improve this answer























          • Cheers dude, excellent.
            – Lost Modernity
            Nov 9 at 3:51










          • Time-limit :(, will do when possible.
            – Lost Modernity
            Nov 9 at 3:53













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          In MySQL versions earlier than 8+, we can use a subquery:



          SELECT * FROM User u
          INNER JOIN
          (
          SELECT f.followee_id, COUNT(*) AS cnt
          FROM Follow f
          GROUP BY f.followee_id
          ) t
          ON u.id = t.followee_id
          WHERE
          t.cnt = (SELECT COUNT(*) FROM Follow
          GROUP BY followee_id ORDER BY COUNT(*) DESC LIMIT 1);


          In MySQL 8+ or later, we can take advantage of the RANK analytic function:



          WITH cte AS (
          SELECT *,
          RANK() OVER (ORDER BY t.cnt DESC) rnk
          FROM User u
          INNER JOIN
          (
          SELECT f.followee_id, COUNT(*) AS cnt
          FROM Follow f
          GROUP BY f.followee_id
          ) t
          ON u.id = t.followee_id
          )

          SELECT *
          FROM cte
          WHERE rnk = 1;





          share|improve this answer














          In MySQL versions earlier than 8+, we can use a subquery:



          SELECT * FROM User u
          INNER JOIN
          (
          SELECT f.followee_id, COUNT(*) AS cnt
          FROM Follow f
          GROUP BY f.followee_id
          ) t
          ON u.id = t.followee_id
          WHERE
          t.cnt = (SELECT COUNT(*) FROM Follow
          GROUP BY followee_id ORDER BY COUNT(*) DESC LIMIT 1);


          In MySQL 8+ or later, we can take advantage of the RANK analytic function:



          WITH cte AS (
          SELECT *,
          RANK() OVER (ORDER BY t.cnt DESC) rnk
          FROM User u
          INNER JOIN
          (
          SELECT f.followee_id, COUNT(*) AS cnt
          FROM Follow f
          GROUP BY f.followee_id
          ) t
          ON u.id = t.followee_id
          )

          SELECT *
          FROM cte
          WHERE rnk = 1;






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 9 at 3:52

























          answered Nov 9 at 3:50









          Tim Biegeleisen

          209k1380129




          209k1380129












          • Cheers dude, excellent.
            – Lost Modernity
            Nov 9 at 3:51










          • Time-limit :(, will do when possible.
            – Lost Modernity
            Nov 9 at 3:53


















          • Cheers dude, excellent.
            – Lost Modernity
            Nov 9 at 3:51










          • Time-limit :(, will do when possible.
            – Lost Modernity
            Nov 9 at 3:53
















          Cheers dude, excellent.
          – Lost Modernity
          Nov 9 at 3:51




          Cheers dude, excellent.
          – Lost Modernity
          Nov 9 at 3:51












          Time-limit :(, will do when possible.
          – Lost Modernity
          Nov 9 at 3:53




          Time-limit :(, will do when possible.
          – Lost Modernity
          Nov 9 at 3:53


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53219528%2fselecting-rows-based-upon-derived-value-maximum%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

          Guess what letter conforming each word

          Port of Spain

          Run scheduled task as local user group (not BUILTIN)