Statistics with Window Functions
Now that we understand how window functions work, we can start using them to calculate useful statistics, such as ranks, percentiles, and rolling statistics.
In the following table, we have summarized a variety of statistical functions that are useful. It is also important to emphasize again that all aggregate functions can also be used as window functions (AVG
, SUM
, COUNT
, and so on):
Figure 5.10: Statistical window functions
Exercise 17: Rank Order of Hiring
ZoomZoom would like to promote salespeople at their regional dealerships to management and would like to consider tenure in their decision. Write a query that will rank the order of users according to their hire date for each dealership:
- Open your favorite SQL client and connect to the
sqlda
database. - Calculate a rank for every salesperson, with a rank of 1 going to the first hire, 2 to the second hire, and so on, using the
RANK()
function:SELECT *,...