Та нэгээс илүү Yahoo messenger хаягтай юу? Нэг цаг хугацаанд давхар ID-аар онлайнд нэвтрэхийг хусэж байна уу? Үүнд ямар ч нэмэлт хувилбар татаж авах шаардлагагүй бөгөөд зөвхөн энэ бяцхан зөвлөмж л танд тус болно.
2011/12/12
2011/12/09
2011/12/08
АГРЕГАТ ФУНКЦ
1. SQL COUNT() Function
The COUNT() function returns the number of rows that matches a specified criteria.
SQL COUNT(column_name) Syntax
The COUNT(column_name) function returns the number of values (NULL values will not be counted) of the specified column:
SELECT COUNT(column_name) FROM table_name
SQL COUNT(*) Syntax
The COUNT(*) function returns the number of records in a table:
SELECT COUNT(*) FROM table_name
SQL COUNT(DISTINCT column_name) Syntax
The COUNT(DISTINCT column_name) function returns the number of distinct values of the specified column:
SELECT COUNT(DISTINCT column_name) FROM table_name
Note: COUNT(DISTINCT) works with ORACLE and Microsoft SQL Server, but not with Microsoft Access.
SQL COUNT(column_name) Example
We have the following "Orders" table:
O_Id
|
OrderDate
|
OrderPrice
|
Customer
|
1
|
2008/11/12
|
1000
|
Hansen
|
2
|
2008/10/23
|
1600
|
Nilsen
|
3
|
2008/09/02
|
700
|
Hansen
|
4
|
2008/09/03
|
300
|
Hansen
|
5
|
2008/08/30
|
2000
|
Jensen
|
6
|
2008/10/04
|
100
|
Nilsen
|
Now we want to count the number of orders from "Customer Nilsen".
We use the following SQL statement:
SELECT COUNT(Customer) AS CustomerNilsen FROM Orders
WHERE Customer='Nilsen'
The result of the SQL statement above will be 2, because the customer Nilsen has made 2 orders in total:
CustomerNilsen
|
2
|
SQL COUNT(*) Example
If we omit the WHERE clause, like this:
SELECT COUNT(*) AS NumberOfOrders FROM Orders
The result-set will look like this:
NumberOfOrders
|
6
|
which is the total number of rows in the table.
SQL COUNT(DISTINCT column_name) Example
Now we want to count the number of unique customers in the "Orders" table.
We use the following SQL statement:
SELECT COUNT(DISTINCT Customer) AS NumberOfCustomers FROM Orders
The result-set will look like this:
NumberOfCustomers
|
3
|
which is the number of unique customers (Hansen, Nilsen, and Jensen) in the "Orders" table.
2. SQL AVG() Function
The AVG() Function
The AVG() function returns the average value of a numeric column.
SQL AVG() Syntax
SELECT AVG(column_name) FROM table_name
SQL AVG() Example
We have the following "Orders" table:
O_Id
|
OrderDate
|
OrderPrice
|
Customer
|
1
|
2008/11/12
|
1000
|
Hansen
|
2
|
2008/10/23
|
1600
|
Nilsen
|
3
|
2008/09/02
|
700
|
Hansen
|
4
|
2008/09/03
|
300
|
Hansen
|
5
|
2008/08/30
|
2000
|
Jensen
|
6
|
2008/10/04
|
100
|
Nilsen
|
Now we want to find the average value of the "OrderPrice" fields.
We use the following SQL statement:
SELECT AVG(OrderPrice) AS OrderAverage FROM Orders
The result-set will look like this:
OrderAverage
|
950
|
Now we want to find the customers that have an OrderPrice value higher than the average OrderPrice value.
We use the following SQL statement:
SELECT Customer FROM Orders WHERE OrderPrice>(SELECT AVG(OrderPrice) FROM Orders)
The result-set will look like this:
Customer
|
Hansen
|
Nilsen
|
Jensen
|
3. SQL SUM() Function
The SUM() Function
The SUM() function returns the total sum of a numeric column.
SQL SUM() Syntax
SELECT SUM(column_name) FROM table_name
SQL SUM() Example
We have the following "Orders" table:
O_Id
|
OrderDate
|
OrderPrice
|
Customer
|
1
|
2008/11/12
|
1000
|
Hansen
|
2
|
2008/10/23
|
1600
|
Nilsen
|
3
|
2008/09/02
|
700
|
Hansen
|
4
|
2008/09/03
|
300
|
Hansen
|
5
|
2008/08/30
|
2000
|
Jensen
|
6
|
2008/10/04
|
100
|
Nilsen
|
Now we want to find the sum of all "OrderPrice" fields".
We use the following SQL statement:
SELECT SUM(OrderPrice) AS OrderTotal FROM Orders
The result-set will look like this:
OrderTotal
|
5700
|
4. SQL MAX() Function
The MAX() Function
The MAX() function returns the largest value of the selected column.
SQL MAX() Syntax
SELECT MAX(column_name) FROM table_name
SQL MAX() Example
We have the following "Orders" table:
O_Id
|
OrderDate
|
OrderPrice
|
Customer
|
1
|
2008/11/12
|
1000
|
Hansen
|
2
|
2008/10/23
|
1600
|
Nilsen
|
3
|
2008/09/02
|
700
|
Hansen
|
4
|
2008/09/03
|
300
|
Hansen
|
5
|
2008/08/30
|
2000
|
Jensen
|
6
|
2008/10/04
|
100
|
Nilsen
|
Now we want to find the largest value of the "OrderPrice" column.
We use the following SQL statement:
SELECT MAX(OrderPrice) AS LargestOrderPrice FROM Orders
The result-set will look like this:
LargestOrderPrice
|
2000
|
5. SQL MIN() Function
The MIN() Function
The MIN() function returns the smallest value of the selected column.
SQL MIN() Syntax
SELECT MIN(column_name) FROM table_name
SQL MIN() Example
We have the following "Orders" table:
O_Id
|
OrderDate
|
OrderPrice
|
Customer
|
1
|
2008/11/12
|
1000
|
Hansen
|
2
|
2008/10/23
|
1600
|
Nilsen
|
3
|
2008/09/02
|
700
|
Hansen
|
4
|
2008/09/03
|
300
|
Hansen
|
5
|
2008/08/30
|
2000
|
Jensen
|
6
|
2008/10/04
|
100
|
Nilsen
|
Now we want to find the smallest value of the "OrderPrice" column.
We use the following SQL statement:
SELECT MIN(OrderPrice) AS SmallestOrderPrice FROM Orders
The result-set will look like this:
SmallestOrderPrice
|
100
|
2011/12/02
Windows 7 үйлдлийн систем
Windows 7.0 Enterprise/Ultimate. Windows 7 –ийн бүхий л онцлогийг багтаасан хувилбар.
2011/12/01
Subscribe to:
Comments (Atom)