The simplest and most primitive aggregation function in a database

Source: Internet
Author: User

What can make us calm when walking in noisy cities? The voice told me some , "simple" is not necessarily really simple , it is relative , when we grow up, want to keep the innocence of childhood, I think only in the dream by the heart of the hair, because the reality let us all changed, to want to re-find the past things, it may be really difficult.

Like the five simpler functions I'm going to say, but is it really simple? Only you can read it carefully once and then it is possible to understand the meaning, all the problems have a breakthrough , when we find, the simple problem arises spontaneously, on the contrary, the simple question is your heart forever lingering shadow. So these functions are simply not easy to say, just as all things exist in fits together,

One might ask: what is an aggregate function and how do you use it?

Expert analysis: It is generally necessary to use aggregate functions when doing statistical analysis

When we need to calculate, the first thing we think about is sum () "sum",avg () "averaging",COUNT () "Total record"; The comparison function is max () Maximum and min () min.

Now that we know what they do , let's look at how to use and implement its functions !!!
01.SUM () function

--Query the total number of hours per grade and in ascending order

Select  as  as total number of hours  from Subjectgroup by Gradeidorder of total hours ASC


02.AVG () function

Select  as average score from    Result

--Check the average score for each student taking the exam

Select  as  as Average score  from Resultgroup by Studentno

--Check the average score for each course and sort in descending order

Select  as  as Average score  from Resultgroup by subjectidorder per cent desc


--Check the average age of each grade student:

Select  as  as average age  from Studentgroup by Gradeid
--the average pass-through student record for the students who are inquiring for the grade test, in descending order select as as  average  from Resultgroup by studentnohaving  avg (studentresult)>ORDER by average DESC

-- Check the exam time to 9-9 the average pass of the course
--average number of courses in all courses

Select Subjectid as as    All courses average  from Resultwhere examdate>='2009-9-9' and examdate<'2009-9-10'  GROUP by subjectidhaving AVG (studentresult)>


03.MAX () function

Select  as  from Result


04.MIN () function

  Select  as  from Result

05.COUNT () function

count (): Counts the total number of data in a single table

For example:

--Check the number of courses that exceed the number of hours per year

Select  as  as Total Courses  from Subject where classhour>=GROUP by Gradeid

--statistics on at least one failing student number and number.

Select  as  as number of  from Result where studentresult<GROUP by Studentno

--Check the total number of students in each grade including Beijing

Select  as  as Total number  from Student where ' % BEIJING% ' GROUP BY Gradeid

Finally, there are a few things to remember to keep in mind for five functions:

--If there is a group by keyword in the statement, then the select can only be followed by columns that appear after group BY, or aggregate functions
--max () min () count (), SUM (), AVG ()

Finally, I hope that my work can make you satisfied, if there are shortcomings, please do not skimp on your advice, only so that my work can satisfy you; because the missing one will always give the next supplement, make it better.

The simplest and most primitive aggregation function in a database

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.