Home Sql SQL Aggregate Functions Group By



SQL Aggregate Functions Group By
Posted on: June 17, 2009 at 12:00 AM
SQL Aggregate Functions Group By return the aggregate functions (like SUM) of the different numeric field for each individual group of column values.

SQL Aggregate Functions Group By

     

SQL Aggregate Functions Group By return the aggregate functions (like SUM) of the different numeric field for each individual group of column values.

Understand with Example

The Tutorial grasp you with the use of 'SQL Aggregate Functions Group By'. In this example, we create a table 'Stu_Table'. The create table statement create a table 'Stu_Table'  

Create Table Stu_Table

create table Stu_Table(Stu_Id varchar(2), Stu_Name varchar(15), 
Stu_Class varchar(10),sub1 integer(2),sub2 integer(2),
sub3 integer(2),sub4 integer(2),sub5 integer(2));

Insert Value Into Stu_Table

The insert into statement add the records or rows to the table. 

insert into Stu_Table values(1, 'Komal', 10, 23, 34, 45, 56, 67);
insert into Stu_Table values(2, 'Ajay', 10, 34, 45, 56, 67, 78);
insert into Stu_Table values(3, 'Rakesh', 10, 23, 43, 45, 45, 34);
insert into Stu_Table values(4, 'Bhanu', 10, 56, 45, 67, 34, 54);
insert into Stu_Table values(5, 'Santosh', 10, 56, 67, 56, 67, 56);
insert into Stu_Table values(6, 'Tanuj', 10, 56, 45, 56, 56, 45);

Stu_Table

+--------+----------+-----------+------+------+------+------+------+
| Stu_Id | Stu_Name | Stu_Class | sub1 | sub2 | sub3 | sub4 | sub5 |
+--------+----------+-----------+------+------+------+------+------+
| 1      | Komal    | 10        | 23   | 34   | 45   | 56   | 67   |
| 2      | Ajay     | 10        | 34   | 45   | 56   | 67   | 78   |
| 3      | Rakesh   | 10        | 23   | 43   | 45   | 45   | 34   |
| 4      | Bhanu    | 10        | 56   | 45   | 67   | 34   | 54   |
| 5      | Santosh  | 10        | 56   | 67   | 56   | 67   | 56   |
| 6      | Tanuj    | 10        | 56   | 45   | 56   | 56   | 45   |
+--------+----------+-----------+------+------+------+------+------+

Query

The given below Query returns you the records enlisted in select statement and sum of different numeric field for each individual group of column values in the table 'Stu_Table'.

select stu_id, stu_name, sub1, sub2, sub3, sub4, sub5,
sum( sub1 + sub2 + sub3 + sub4 + sub5) as total 
from stu_table group by stu_id;

Result

+--------+----------+------+------+------+------+------+-------+
| stu_id | stu_name | sub1 | sub2 | sub3 | sub4 | sub5 | total |
+--------+----------+------+------+------+------+------+-------+
| 1      | Komal    | 23   | 34   | 45   | 56   | 67   | 225   |
| 2      | Ajay     | 34   | 45   | 56   | 67   | 78   | 280   |
| 3      | Rakesh   | 23   | 43   | 45   | 45   | 34   | 190   |
| 4      | Bhanu    | 56   | 45   | 67   | 34   | 54   | 256   |
| 5      | Santosh  | 56   | 67   | 56   | 67   | 56   | 302   |
| 6      | Tanuj    | 56   | 45   | 56   | 56   | 45   | 258   |
+--------+----------+------+------+------+------+------+-------+

Related Tags for SQL Aggregate Functions Group By:
sqlcaspfunctionsfunctionfuniotutorialaggregateriagroupuncwithtoeituseasfunctorsspatrasgretutorssrithgagatgraspono


More Tutorials from this section

Ask Questions?    Discuss: SQL Aggregate Functions Group By  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.