The number of values in the SQL Server statistics field is within certain ranges

Source: Internet
Author: User
Tags ranges

There is a table test as follows:

Create Table  intIdentity(1,1primarykeyint  )

Insert data:

Insert  intoTest (NUM)Values(1);Insert  intoTest (NUM)Values(2);Insert  intoTest (NUM)Values(8);Insert  intoTest (NUM)Values( the);Insert  intoTest (NUM)Values( A);Insert  intoTest (NUM)Values( -);Insert  intoTest (NUM)Values( -);Insert  intoTest (NUM)Values( -);Insert  intoTest (NUM)Values( -);Insert  intoTest (NUM)Values(5);Insert  intoTest (NUM)Values(6);Insert  intoTest (NUM)Values(7);Insert  intoTest (NUM)Values( -);Insert  intoTest (NUM)Values( -);Insert  intoTest (NUM)Values(9);Insert  intoTest (NUM)Values(Ten);Insert  intoTest (NUM)Values( One);Insert  intoTest (NUM)Values( A);Insert  intoTest (NUM)Values( +);Insert  intoTest (NUM)Values( -);Insert  intoTest (NUM)Values(3);Insert  intoTest (NUM)Values(4);Insert  intoTest (NUM)Values( +);Insert  intoTest (NUM)Values( -);Insert  intoTest (NUM)Values( -);Insert  intoTest (NUM)Values( -);
View Code

Question: Please use an SQL statement query to count the number of num in the 1~6, 10~17, 19~20 three ranges, respectively?

The solution is as follows:

Select COUNT( Case  whenNumbetween 1  and6 Then 1 End) as [1-6],COUNT( Case  whenNumbetween Ten  and  -  Then 2 End) as [10-17],COUNT( Case  whenNumbetween  +  and  -  Then 3 End) as [19-20] fromTest

If the problem is to ask the 6~10,11~15, the number of multiples (regular), then you can write:

Select COUNT (*fromgroup by num/5

The number of values in the SQL Server statistics field is within certain ranges

Related Article

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.