w3resource

AdventureWorks Database: Find different sales quotas for a employee over next years

SQL Query - AdventureWorks: Exercise-109 with Solution

109. From the following table write a query in SQL to return the difference in sales quotas for a specific employee over subsequent years. Return BusinessEntityID, year, SalesQuota, and the salesquota coming in next row.

Sample table: Sales.SalesPersonQuotaHistory


Click to view Full table

Sample Solution:

SELECT BusinessEntityID, date_part('year',QuotaDate) AS SalesYear, SalesQuota AS CurrentQuota,   
    LEAD(SalesQuota, 1,0) OVER (ORDER BY date_part('year',QuotaDate)) AS NextQuota  
FROM Sales.SalesPersonQuotaHistory  
WHERE BusinessEntityID = 277 AND date_part('year',QuotaDate) IN ('2011','2012'); 

Sample Output:

businessentityid|salesyear|currentquota|nextquota|
----------------+---------+------------+---------+
             277|   2011.0|      565000|   872000|
             277|   2011.0|      872000|   846000|
             277|   2011.0|      846000|   952000|
             277|   2012.0|      952000|  1600000|
             277|   2012.0|     1600000|  1352000|
             277|   2012.0|     1352000|   839000|
             277|   2012.0|      839000|        0|

SQL AdventureWorks Editor:

Practice Online


Contribute your code and comments through Disqus.

Previous: Find the statistical variance of the sales quota for each quarter.
Next: Compare year-to-date sales between employees for specific terrotery .


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