Tải bản đầy đủ (.pdf) (11 trang)

Tài liệu Using SQL phần 3 ppt

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (36.91 KB, 11 trang )


Figure 3.16: Using the DISTINCT keyword to retrieve distinct Country column values
As you can see, the SELECT statement only displays Country column values that are
unique: duplicate values are eliminated. If you didn't include the DISTINCT keyword,
then all the Country column values would be displayed.
Combining Retrieved Rows From SELECT Statements
You use the UNION operator to combine retrieved rows from SELECT statements into
one set of rows. For example, the following SELECT statement uses the UNION operator
to combine the retrieved rows from two SELECT statements that retrieve rows from the
Products table; the first retrieves rows where the ProductID is less than or equal to 5, and
the second retrieves rows where the ProductName starts with Queso:
(SELECT ProductID, ProductName, QuantityPerUnit, UnitPrice
FROM Products
WHERE ProductID <= 5)
UNION
(SELECT ProductID, ProductName, QuantityPerUnit, UnitPrice
FROM Products
WHERE ProductName LIKE 'Queso%');
Figure 3.17 shows the results of this statement.

Figure 3.17: Using the UNION operator to combine retrieved rows from two SELECT
statements
Dividing Retrieved Rows into Blocks
You use the GROUP BY clause to divide retrieved rows into blocks. You can think of a
block as a group of rows that have been condensed into one row. For example, let's say
you grouped the SupplierID column of the rows from the Products table. You would get
one row for every row that had the same SupplierID column value. The following
SELECT statement uses the GROUP BY clause to divide the SupplierID column values
into blocks:
SELECT SupplierID
FROM Products


GROUP BY SupplierID;

This SELECT statement displays one row for each group of rows that have the same
SupplierID column value. You can get the number of rows in each block using the
COUNT() function. COUNT() is one of the functions that come built into SQL Server,
and is known as an aggregate function because it can operate on more than one row at a
time. You use COUNT(*) to get the number of rows, as shown in the following example
that retrieves the SupplierID and number of rows for each group of SupplierID column
values:
SELECT SupplierID, COUNT(*)
FROM Products
GROUP BY SupplierID;
Figure 3.18 shows the results of this SELECT statement.

Figure 3.18: Using the GROUP BY clause to divide rows into blocks
You'll learn more about the various SQL Server functions in the next chapter
.
Restricting Retrieved Groups of Rows
You use the HAVING clause to restrict the groups of rows retrieved by the GROUP BY
clause. For example, the following SELECT statement uses the HAVING clause to
restrict the group of rows returned to those that have more than 4 rows in each group:
SELECT SupplierID, COUNT(*)
FROM Products
GROUP BY SupplierID
HAVING COUNT(*) > 4;
Figure 3.19
shows the results of this SELECT statement.

Figure 3.19: Using the HAVING clause to restrict retrieved groups of rows
Specifying the Display Name for a Column and Aliasing a Table

You can use the AS clause to specify the name of a column when it is displayed in the
output from a SELECT statement. You might want to do this when you need to display
more friendly names or descriptive names for columns. For example, the following
SELECT statement uses the AS clause to set the display name of the ProductName
column to Product, and the UnitPrice column to Price for each unit:
SELECT ProductName AS Product, UnitPrice AS 'Price for each unit'
FROM products;
Figure 3.20
shows the results of this SELECT statement.

Figure 3.20: Using the AS clause to specify the display name for columns
You can also use the AS clause to alias a table. You might want to do this if your table
names are long. The following example uses the AS clause to alias the Customers and
Orders tables as Cust and Ord respectively:
SELECT Cust.CustomerID, CompanyName, Address, OrderID, ShipAddress
FROM Customers AS Cust, Orders AS Ord
WHERE Cust.CustomerID = Ord.CustomerID
AND Cust.CustomerID = 'ALFKI';
Performing Computations Based on Column Values
You typically use calculated fields to perform computations based on column values. For
example, you might want to use a calculated field to compute the effect of increasing the
UnitPrice column of the Products table by 20 percent. The following SELECT statement
shows this:
SELECT UnitPrice * 1.20
FROM Products
WHERE ProductID = 1;
This example returns 21.600000. The new unit price is calculated using UnitPrice * 1.20.
This is an increase of 20 percent over the current unit price.
The next example concatenates the ContactName and ContactTitle columns from the
Customers table for the row where the CustomerID equals ALFKI:

SELECT ContactName + ', ' + ContactTitle
FROM Customers
WHERE CustomerID = 'ALFKI';

×