For Aggregate functions (such as SUM), you often need to add the GROUPBY statement. The GROUPBY statement is used in combination with the aggregate function to group result sets based on one or more columns. SQLGROUPBY syntax SELECTcolumn_name, aggregate_function (column_name) fromtable_namewherecolumn_nameaggregate Aggregate functions (such as SUM) often need to add a group by statement.
Group by statement
The group by statement is used in combination with the aggregate function to GROUP result sets based on one or more columns.
SQL GROUP BY syntax
SELECT column_name, aggregate_function (column_name)
FROM table_name
WHERE column_name operator value
Group by column_name
SQL GROUP BY instance
We have the following "Orders" table:
O_Id OrderDate OrderPrice Customer
1 2008/12/29 1000 Bush
2 2008/11/23 1600 Carter
3 2008/10/05 700 Bush
4 2008/09/28 300 Bush
5 2008/08/06 2000 Adams
6 2008/07/21 100 Carter
Now we want to find the total amount (total order) for each customer ).
We want to use the group by statement to combine the customer.
We use the following SQL statement:
SELECT Customer, SUM (OrderPrice) FROM Orders
Group by Customer
The result set is similar to the following:
Customer SUM (OrderPrice)
George Bush 2000
Carter, 1700
Adams 2000
Great, right?
Let's take a look at what will happen if group by is omitted:
SELECT Customer, SUM (OrderPrice) FROM Orders result set is similar to this:
Customer SUM (OrderPrice)
George Bush 5700
Carter, 5700
George Bush 5700
George Bush 5700
Adams 5700
Carter, 5700
The above result set is not what we need.
So why can't I use the preceding SELECT statement? The preceding SELECT statement specifies two columns (Customer and SUM (OrderPrice )). "SUM (OrderPrice)" returns a separate value (total of the "OrderPrice" column ), "Customer" returns six values (each value corresponds to each row in the "Orders" table ). Therefore, we cannot get the correct results. However, you can see that the group by statement solves this problem.
Group by more than one column
You can also apply the group by statement to more than one column, as shown in the following figure:
SELECT Customer, OrderDate, SUM (OrderPrice) FROM Orders
Group by Customer, OrderDate
Integrated instance
> Create table employee (
2> ID int,
3> name nvarchar (10 ),
4> salary int,
5> start_date datetime,
6> city nvarchar (10 ),
7> region char (1 ))
8> GO
1>
2> insert into employee (ID, name, salary, start_date, city, region)
3> values (1, 'jason ', 40420, '2014/1/94', 'New York ', 'w ')
4> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (2, 'Robert ', 14420, '2017/95', 'vancouver', 'n ')
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (3, 'celia ', 24020, '2017/96', 'toronto ', 'w ')
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (4, 'linda ', 40620, '2014/1/97', 'New York ', 'n ')
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (5, 'David ', 80026, '2014/1/98', 'vancouver ', 'w ')
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (6, 'James ', 70060, '2017/99', 'toronto', 'n ')
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (7, 'alison ', 90620, '2014/1/00', 'New York', 'w ')
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (8, 'chris ', 26020, '2014/1/01', 'vancouver ', 'n ')
3> GO
(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (9, 'Mary ', 60020, '2017/02', 'toronto ', 'w ')
3> GO
(1 rows affected)
1>
2> * from employee
3> GO
ID name salary start_date city region
-----------------------------------------------------------------------
1 Jason 40420 00:00:00. 000 New York W
2 Robert 14420 00:00:00. 000 Vancouver N
3 Celia 24020 00:00:00. 000 Toronto W
4 Linda 40620 00:00:00. 000 New York N
5 David 80026 00:00:00. 000 Vancouver W
6 James 70060 00:00:00. 000 Toronto N
7 Alison 90620 00:00:00. 000 New York W
8 Chris 26020 00:00:00. 000 Vancouver N
9 Mary 60020 00:00:00. 000 Toronto W
(9 rows affected)
1>
2> -- group by clause with an aggregator 'sum ()'.
3>
4> SELECT region, SUM (Salary)
5> FROM Employee
6> where id between 1 AND 10
7> group by Region
8> GO
Region
-----------------
N 151120
W 295106
(2 rows affected)
1>
2>
3> drop table employee
4> GO
1>