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

  • Home
  • SEARCH
  • 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 3286216
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:21:18+00:00 2026-05-17T20:21:18+00:00

I have an issue with an SQL Query. Lets take this example data itemID

  • 0

I have an issue with an SQL Query. Lets take this example data

itemID  catID  attrib1  attrib2
  1       1       10       5   
  2       1       10       7
  3       1        5      10
  4       2       18      15

I want to return the best item for each category (with attrib1 having priority over attrib2)

Obviously, SELECT catID, MAX(attrib1), MAX(attrib2) FROM test_table GROUP BY catID doesn’t work since it will return 10 & 10 for the 1st cat.

So is there anyway to tell MySQL to select max value from attrib2 row but only consider the ones where attrib1 is also max value ? i.e return the following data

 catID  attrib1  attrib2
   1       10       7   
   2       18      15
  • 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-05-17T20:21:18+00:00Added an answer on May 17, 2026 at 8:21 pm

    You can get the best attrib1 values, and then join in the attrib2 values and get the best of those for each attrib1 value:

    select t2.catID, t2.attrib1, max(t2.attrib2)
    from
    (
      select catID, max(attrib1) as attrib1
      from test_table
      group by catID
    ) t1
    inner join test_table t2 on t2.catID = t1.catID and t2.attrib1 = t1.attrib1
    group by t2.catID, t2.attrib1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently trying to run a LINQ query over a MS SQL database. This
I have a query in SQL Server that I am trying to convert to
I seem to be facing a strange issue in SQL 2008. I have a
I have found this example on StackOverflow: var people = new List<Person> { new
I have two issues both related to (I believe) my SQL Server setup. I
I have been having some issues with LINQ-To-SQL around memory usage. I'm using it
I have an issue that is driving me a bit nuts: Using a UserProfileManager
We have an issue using the PEAR libraries on Windows from PHP . Pear
We have an issue related to a Java application running under a (rather old)
We have an issue on our page whereby the first time a button posts

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.