SQL statement grouping by age group count _ MySQL

Source: Internet
Author: User
The SQL statement counts the number of people by age group. bitsCN.com


Number of SQL statements grouped by age group

Java code

Create table # t (Uname varchar (10), age int)

Insert # t

Select 'ah', 19 union all

Select 'info', 23 union all

Select 'baby', 31 union all

Select 'random Wo', 21 union all

Select 'mimeters ', 6

Select nnd as 'age group', count (*) as 'number of people' from (

Select

Case

When age> = 1 and age <= 10 then '1-10'

When age> = 11 and age <= 20 then '11-20'

When age> = 21 and age <= 30 then '21-30'

When age> = 31 and age <= 40 then '31-40'

End

As nnd, uname from # t

) A group by nnd

BitsCN.com

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.