AdventureWorks Database: Find the statistical variance of the sales quota for each quarter

SQL Query - AdventureWorks: Exercise-108 with Solution

108. From the following table write a query in SQL to return the statistical variance of the sales quota values for a salesperson for each quarter in a calendar year. Return quotadate, quarter, SalesQuota, and statistical variance. Order the result set in ascending order on quarter.

Sample table: Sales.SalesPersonQuotaHistory

Click to view Full table

Sample Solution:

SELECT quotadate AS Year, date_part('quarter',quotadate) AS Quarter, SalesQuota AS SalesQuota,  
       var_pop(SalesQuota) OVER (ORDER BY date_part('year',quotadate), date_part('quarter',quotadate)) AS Variance  
FROM sales.salespersonquotahistory  
WHERE businessentityid = 277 AND date_part('year',quotadate) = 2012  
ORDER BY date_part('quarter',quotadate);

Sample Output:

year                   |quarter|salesquota|variance             |
2012-02-29 00:00:00.000|    1.0|    952000|                    0|
2012-05-30 00:00:00.000|    2.0|   1600000|104976000000.00000000|
2012-08-30 00:00:00.000|    3.0|   1352000| 71267555555.55555556|
2012-11-30 00:00:00.000|    4.0|    839000| 93529187500.00000000|

SQL AdventureWorks Editor:

Practice Online

Contribute your code and comments through Disqus.

Previous: Find the sales quota difference between the current and the first and last quarter.
Next: Find different sales quotas for a employee over next years.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

Follow us on Facebook and Twitter for latest update.