w3resource

AdventureWorks Database: Compute the variance of sales quota for each quarter in a year

SQL Query - AdventureWorks: Exercise-96 with Solution

96. From the following table write a query in SQL to compute the statistical variance of the sales quota values for each quarter in a calendar year for a sales person. Return year, quarter, salesquota and variance of salesquota.

Sample table: sales.salespersonquotahistory


Click to view Full table

Sample Solution:

SELECT quotadate AS Year, date_part('quarter',quotadate) AS Quarter, SalesQuota AS SalesQuota,  
       variance(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|                     |
2012-05-30 00:00:00.000|    2.0|   1600000|209952000000.00000000|
2012-08-30 00:00:00.000|    3.0|   1352000|106901333333.33333333|
2012-11-30 00:00:00.000|    4.0|    839000|124705583333.33333333|

SQL AdventureWorks Editor:

Practice Online


Contribute your code and comments through Disqus.

Previous: Find the total products ordered in each specified orders.
Next: Populate the variance of all unique values and all values.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

SQL: Tips of the Day

Convert Timestamp to date in MySQL Query:

DATE_FORMAT(FROM_UNIXTIME(`user.registration`), '%e %b %Y') AS 'date_formatted'

Database: MySQL

Ref : https://bit.ly/3EJPnMQ