Group by query in MSSQL TableIn our student database we have seen how count query works. Now we will discuss how to display total number of records based on GROUP By SQL command. You can read more on GROUP BY command here.
Let us try to display total number of students in each class. Here we will display class name and number of students in the class.
You can read the basic SELECT QUERY here , we will only change the sql part here.
rs1.open " select COUNT(class) as no,class from student group by class " , connWithin the Do While loop we will keep this code to display the data.
Response.Write rs1("class") & rs1("no") & "<br>"
GROUP BY command with SUMWe can find out total mark of students in each class by using SUM and GROUP BY sql commands. Here is the query and display part
rs1.open " select SUM(mark) as no,class FROM student GROUP BY class " , conn
GROUP BY command with AVGSame way we can get teh average mark in each class by using AVG command with GROUP BY
rs1.open " select AVG(mark) as no,class FROM student GROUP BY class " , conn
GROUP BY command with MAX and MINSame way we can find out the highest mark in each class by using MAX command and the minimun mark by using MIN query
rs1.open " select MAX(mark) as no,class FROM student GROUP BY class " , conn