SQL query records for MAX () Max () in a category field in a table

Source: Internet
Author: User
Tags mssql cpu usage

The problem is:

the database has a table code with a click Field Click_num and a category field kind and other information fields.

now to find out the most hits in each category of the record, if it is 10 categories, then the result should be 10 records,

if the maximum number of hits is two the same as one.

after n searches, n-times the solution SQL statement on the Internet, finally found an elegant and results-based SQL, this is a blog author found in the official MySQL document.

Can't help collecting, for later use.

  1. Select id,kind,click_num from code as a
  2. where click_num= (Select max(b.click_num)
  3. From code as b
  4. where a.kind = B.kind
  5.                   );

Special attention:

This statement is tested on SQLite and is fast and normal. But on my machine on the MySql5.0 on the execution of the crash, other versions of MySQL do not know what the situation.

The above statement does not satisfy my second requirement: if the maximum number of hits in a category has two identical records, only one.

Here are my additional workarounds:

filter, ensure that each category has only one, (Kind,id to all columns, the result is the corresponding record)

  1. Select *
  2. from select from as
  3.                             < Span style= "color: #0000FF; font-weight:bold;background-color:inherit;" >where select max
  4.                                                  from  code   b  
  5. where a.kind = B.kind
  6.                                              )  
  7. ) as a
  8. Group by kind


Follow-up:

One of the biggest problems with MySQL in the past is that in the WHERE clause of a query, if you include a query for the table in the previous select, the CPU usage will soar and the MySQL service stops. Can be said to be a self-connected query problem, do not know whether this is a bug,5.0,5.1 version of the problem, look back to see the above SQL statement just conforms to this problem, the solution is to query the table into a temporary table or view in short, and the previous select table is the same table, Then the problem is solved, so that the above SQL can be slightly modified to run on MySQL.

Modify the process:

1. Create a view max_click that stores the maximum number of clicks per category

  1. create view as select max as
  2. From code
  3. Group by kind;

2. Filter to ensure only one record per category

  1. Select *
  2. From (Select id,kind,click_num
  3. From code
  4. where click_num = ( Select b.click_num
  5. From max_click as b
  6. where code.kind = B.kind
  7.                            )  
  8. ) as a
  9. Group by kind;


Normal operation, speed 0.78 seconds. (altogether 23,000 data in the table) okay! Done!

the first SQL running on MSSQL do not know the line, I think in SQLite can be normal in MSSQL should be no problem, it seems that MySQL needs to improve a lot of Ah, also is open source SQLite can do, and sqlite so lightweight on hundreds of KB, MySQL should be able to do more.

SQL query records for MAX () Max () in a category field in a table

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.