Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7835501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:39:46+00:00 2026-06-02T13:39:46+00:00

I have a table that includes names, items, types, and a count per item.

  • 0

I have a table that includes names, items, types, and a count per item. I need to find a way to determine the name that has the MAX count in each type.

Example table:

---------------------------------------
| name | item     | type      | count |
| ------------------------------------|
| Dave | carrot   | vegetable | 2     |
| Dave | broccoli | vegetable | 3     |
| Tom  | spinach  | vegetable | 2     |
| Jon  | swiss    | cheese    | 3     |
| Mark | cheddar  | cheese    | 5     |
| Jon  | cheddar  | cheese    | 6     |
| Tony | onion    | vegetable | 3     |

I want to find the names of each person with the highest SUM(count) in each type. This is my expected result:

----------------------------
| name | type      | count |
| -------------------------|
| Dave | vegetable | 5     |
| Jon  | cheese    | 9     |

I’m trying to see if there is an elegant way of doing it other than queries for every name and then manually calculating the MAX.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T13:39:48+00:00Added an answer on June 2, 2026 at 1:39 pm

    First calculate the totals for each name/category combination. Do this either in a subquery or a view. I’m going to choose a view to reduce code bloat, since it will be needed more than once.

    CREATE VIEW totals AS
    SELECT name, type, SUM(count) AS count
    FROM yourtable
    GROUP BY name, type
    
    ----------------------------
    | name | type      | count |
    | -------------------------|
    | Dave | vegetable | 5     |
    | Tony | vegetable | 3     |
    | Tom  | vegetable | 2     |
    | Jon  | cheese    | 9     |
    | Mark | cheese    | 5     |
    ----------------------------
    

    Now you need to find the maximum count for each type.

    SELECT type, MAX(count) AS max_count
    FROM totals
    GROUP BY type
    
    -------------------------
    | type      | max_count |
    | -----------------------
    | vegetable | 5         |
    | cheese    | 9         |
    -------------------------
    

    Now you just need to query the view totals for all rows where (type, count) is equal to any of the pairs in the second result set. I’ve already done most of the work for you. I’ll leave you to finish it off.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that includes some student group name, lesson time, day names
I have a table of userdata that includes user ID and category IDs. I
I have a table it includes 3 foreign key field like that: My Table:
I have table that contains more than 12 millions of rows. I need to
I have table (call it my_table ) that can be simplified like this: NAME,
I have an excel file with 815 records that includes service name and charge.
I have a CATEGORIES table that links to an ITEMS table (one to many).
I have table that describes association of two other business models. Example: Construction Table
I have table that I insert data with following query (from c# code): INSERT
I have table that contain date and time field. id|date|time ========= 1|01/01/2001|10:45 2|01/02/2002|11:45 3|01/03/2003|12:45

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.