GROUP BY clause in SQL Server
The GROUP BY clause in SQL Server (Transact-SQL) is used in the SELECT statement to retrieve data from multiple records and result groups into 1 or more columns.
GROUP BY clause clause in SQL Server
SELECT bieuthuc1, bieuthuc2, … bieuthuc_n,
ham_tong (bieuthuc)
FROM bang
[WHERE dieukien]
GROUP BY
bieuthuc1, bieuthuc2, . bieuthuc_n;
Variable name or variable value
bieuthuc1, bieuthuc2, . bieuthuc_n
The expression is not within the total function and must be in the GROUP BY clause.
ham_tong
Can be functions like SUM, COUNT, MIN, MAX or AVG.
state
The table to retrieve records from, must have at least 1 table in the FROM clause.
WHERE dieukien
Option. The condition that the record must meet to be selected is in the WHERE clause.
For example - use the SUM function
SELECT ten_sanpham, SUM(soluong) AS 'Tong so luong'
FROM sanpham
GROUP BY te
n_sanpham;
This example uses the SUM calculation function to return the product name and total quantity (by product name).
Because you give a column (ten_sanpham) in the SELECT statement and not in the SUM function, you must use the GROUP BY clause.
For example - use the COUNT function
SELECT relatedly_id, COUNT (*) AS 'So nhan vien'
FROM nhanvien
WHERE ho = 'Anderson'
GROUP BY qua
nly_id;
In this example, the COUNT function will return quanly_id and the number of employees with the last name is Anderson.
For example - use the MIN function
SELECT loai_sanpham, MIN(soluong) AS 'So luong it nhat'
FROM sanpham
GROUP B
Y loai_sanpham;
The GROUP BY clause used with the MIN function above will return the product type and the minimum quantity for that product type.
For example - use the MAX function
SELECT bophan, MAX(luong) AS 'Luong cao nhat'
FROM nhanvien
GROUP b
ophan;
This final example returns the name of each department and the maximum salary in the department.
Previous article: Conditions EXISTS in SQL Server
Lesson: HAVING clause in SQL Server
You should read it
May be interested
- The clause to combine JOIN data in SQLin sql, the join clause is used to combine records from two or more tables in a database using common values from each table.
- How to install software for clients from Windows Server 2012 R2 using Group Policywindows server 2012 r2 includes a feature called software installation and maintenance with the ds, group policy, and windows installer services used to install, maintain, and remove software on your computer. in the following article, network administrator will guide you through the steps to install software for clients from windows server 2012 r2 using group policy.
- Attach database in Microsoft SQL Server 2008this article will illustrate the different usage methods of the 'for attach' clause to overcome the limitations encountered when using sp_attach_db and sp_attach_single_file_db.
- Find out about Managed Group Services Accounts in Windows Server 2012managed service accounts (msa) - managed service account - was introduced in windows server 2008 r2 to automatically manage (or change) the passwords of service accounts.
- Backup for Exchange Server with DPM 2007 - Part 3: Backup processwhen the first replication of dpm server of exchange databases is performed, the synchronization problem will appear at a certain frequency, which is configured for short-term restore points. the protection status of each storage group is displayed in the dpm 2007 administrator console (figure 1).
- Interesting Facts About The Santa Clausethe santa clause is one of the best christmas movies of all time. however, behind this christmas movie are surprising, little-known facts about the making process.
- Deploying IPsec Server and Domain Isolation with Windows Server 2008 Group Policy - Part 1network access protection is a new technology available in windows server 2008, allowing you to control which computers are allowed to connect to other computers on your network.
- Removing the first Exchange 2003 server (Part I)deactivating an old exchange 2003 server is a fairly easy process. but you will need to add some steps if it is the first server installed in an administrative group. the reason is because the first exchange server is in & oacu
- Deploying IPsec Server and Domain Isolation with Windows Server 2008 Group Policy - Part 2in this part 2, we'll move on to the second step, which is the step to install and configure the network policy server, the health registration authority and the subordinate ca.
- ORDER BY command in SQLin sql, the order by clause is used to sort data in ascending order or in descending order on one or more columns.