How does SQL count the number of students in each segment of a course? (In the php Program)

Source: Internet
Author: User
$ Cid $ _ GET [id];
$cid = $_GET['id']; 
Reply content: divide by 10 and use group by and count aggregate again. Let's make SQL into a dynamic language. It's really true-I'm a cainiao and can write it at will ···
DECLARE @ grade TABLE (
[Id] [int] IDENTITY (1, 1) not null,
[CId] [int] not null,
[SId] [int] not null,
[Sgrade] [int] NOT NULL
);

Insert into @ grade (cId, sId, sgrade) VALUES (1, 0, 0 );
Insert into @ grade (cId, sId, sgrade) VALUES (1, 1, 3 );
Insert into @ grade (cId, sId, sgrade) VALUES (1, 2, 6 );
Insert into @ grade (cId, sId, sgrade) VALUES (1, 3, 9 );
Insert into @ grade (cId, sId, sgrade) VALUES (1, 4, 12 );
Insert into @ grade (cId, sId, sgrade) VALUES (1, 5, 15 );
Insert into @ grade (cId, sId, sgrade) VALUES (1, 6, 18 );
Insert into @ grade (cId, sId, sgrade) VALUES (1, 7, 21 );
Insert into @ grade (cId, sId, sgrade) VALUES (1, 8, 24 );
Insert into @ grade (cId, sId, sgrade) VALUES (1, 9, 27 );
Insert into @ grade (cId, sId, sgrade) VALUES (1, 10, 30 );
Insert into @ grade (cId, sId, sgrade) VALUES (1, 11, 33 );
Insert into @ grade (cId, sId, sgrade) VALUES (1, 12, 36 );
Insert into @ grade (cId, sId, sgrade) VALUES (1, 13, 39 );
Insert into @ grade (cId, sId, sgrade) VALUES (1, 14, 42 );
Insert into @ grade (cId, sId, sgrade) VALUES (1,15, 45 );
Insert into @ grade (cId, sId, sgrade) VALUES (1, 16, 48 );
Insert into @ grade (cId, sId, sgrade) VALUES (1, 17, 51 );
Insert into @ grade (cId, sId, sgrade) VALUES (1, 18, 54 );
Insert into @ grade (cId, sId, sgrade) VALUES (1,19, 57 );
Insert into @ grade (cId, sId, sgrade) VALUES (1, 20, 60 );
Insert into @ grade (cId, sId, sgrade) VALUES (1, 21, 63 );
Insert into @ grade (cId, sId, sgrade) VALUES (1, 22, 66 );
Insert into @ grade (cId, sId, sgrade) VALUES (1, 23, 69 );
Insert into @ grade (cId, sId, sgrade) VALUES (1, 24, 72 );
Insert into @ grade (cId, sId, sgrade) VALUES (25-25, 75 );
Insert into @ grade (cId, sId, sgrade) VALUES (1,26, 78 );
Insert into @ grade (cId, sId, sgrade) VALUES (1,27, 81 );
Insert into @ grade (cId, sId, sgrade) VALUES (1,28, 84 );
Insert into @ grade (cId, sId, sgrade) VALUES (1,29, 87 );
Insert into @ grade (cId, sId, sgrade) VALUES (1, 30, 90 );
Insert into @ grade (cId, sId, sgrade) VALUES (1,31, 93 );
Insert into @ grade (cId, sId, sgrade) VALUES (1,32, 96 );
Insert into @ grade (cId, sId, sgrade) VALUES (1, 33, 99 );
Insert into @ grade (cId, sId, sgrade) VALUES (100 );

Select count (sId) [Count], sgrade/10*10 lowersgrade, sgrade/10*10 + 9 uppersgrade
FROM @ grade cs WHERE cs. cId = 1
Group By sgrade/10;

Select count (1) [Count], st * 10 lowersgrade, st * 10 + 9 uppersgrade FROM
(SELECT sId, sgrade/10 st
FROM @ grade cs WHERE cs. cId = 1) t
Group by t. st;

If GO is computed using SQL, I don't know whether there are functions such as frequency. I usually use the remainder. For example, if the group distance is 7, divide the score by 7 to calculate the remainder, and then use the score-remainder/7 to get the group number.

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.