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.



Follow us on Facebook and Twitter for latest update.

SQL: Tips of the Day

Concatenate strings of a string field in a PostgreSQL 'group by' query:

Input:

ID   COMPANY_ID   EMPLOYEE
1    1            Anna
2    1            Bill
3    2            Carol
4    2            Dave
SELECT company_id, string_agg(employee, ', ')
FROM mytable
GROUP BY company_id;

Output:

COMPANY_ID   EMPLOYEE
1            Anna, Bill
2            Carol, Dave

Database: PostgreSQL

Ref: https://bit.ly/2XTiRjq

 





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook