All Type Coding

Search Here

Group By statement in Sql Server.

GROUP BY:

GROUP BY Clause is used in a select statement to collect data across multiple records and group the results by one or more columns.or we can say that.Group By clause is used for grouping the records of the database tables.The  Group By clause creates a single row for each group and this process is called aggregation. Using for  group by clause we have to use at least one aggregate function in Select statement. aggregate_function can be a function such as SUM, COUNT, MIN, MAX, or AVG functions.

Syntax: 
                                  Select Column1,Column2,Column3 from Table_Name
                                  Where Condition
                                  Group By Column1,.........   OR

                                  Select Column1,Column2,Column3 from                                                       Table_Name
                                  Where Condition
                                  Group By Column1,......... 
                                  Order By Column2...... OR

                                  Select Column1,Column2,SUM(Column3)as Total                                       from Table_Name
                                  Where Condition
                                  Group By Column1,......... 

Step1. Create a table 


















Step2. Insert data into the table.Employee1.

                                     Insert into Employee1 values('AAA',5000,'Female')
                                     Insert into Employee1 values('BBB',1000,'male')
                                     Insert into Employee1 values('CCC',4000,'Female')
                                     Insert into Employee1 values('CCC',4000,'Female')
                                     Insert into Employee1 values('EEE',6000,'Female')
                                     Insert into Employee1 values('EEE',6000,'Female')
                                     Insert into Employee1 values(('BBB',1000,'male')






























Step3 Now execute the command with group by .

















Now we can count the number of people which having same name,salary and gender like below.

















We can use more than one aggregate function Now we'll get sum of salary which have tha same name and gender.
















No comments :

Post a Comment