The
GROUP BY
Clause
The GROUP BY phase allows you to arrange the rows returned by the previous logical query proc-
essing phase in groups. The groups are determined by the elements you specify in the GROUP BY
clause. For example, the GROUP BY clause in the query in Listing 2-1 has the elements empid and
YEAR(orderdate).
FROM Sales.Orders
WHERE custid = 71
GROUP BY empid, YEAR(orderdate)
This means that the GROUP BY phase produces a group for each unique combination of employee
ID and order year values that appears in the data returned by the WHERE phase. The expression
YEAR(orderdate) invokes the YEAR function to return only the year part from the orderdate column.
The WHERE phase returned 31 rows, within which there are 16 unique combinations of employee
ID and order year values, as shown here.
empid YEAR(orderdate)
----------- ---------------
1 2006
1 2007
1 2008
2 2006
2 2007
2 2008
3 2007
4 2007
4 2008
5 2007
6 2007
6 2008
7 2007
7 2008
8 2007
9 2006
www.it-ebooks.info
Do'stlaringiz bilan baham: |