The sum of the first five data and all other data of SQL query

Source: Internet
Author: User

Knowledge management feedback System, analysis of Knowledge Retrieval office (Beijing Shanghai Tianjin Hong Kong New York Tokyo .... Suzhou) source, I need to find out which offices are more frequently, drain the top 5, and calculate the sum of the number of queries for all remaining departments.

The database is MySQL.

1 Spell out the top five

Select sum (countas fromgroupby5;

2. Retrieve all other

Theoretically it should be:

Select ' other ',sum(countfromwhere not in (select  from Group  by 5);

However, SQL cannot execute, prompting:
This version of MySQL doesn ' t yet support ' LIMIT & in/all/any/some subquery

You cannot use limit in a subquery, the solution is to set a layer of queries here and spell it out:

Select ' other ',sum(countfromwhere not in (select  from (Selectfromgroupby5 as T);


Succeed in getting results ~

The sum of the first five data and all other data of SQL query

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.