w3resource

AdventureWorks Database: Find the sales quota difference between the current and the first and last quarter

SQL Query - AdventureWorks: Exercise-107 with Solution

107. From the following table write a query in SQL to compute the difference between the sales quota value for the current quarter and the first and last quarter of the year respectively for a given number of employees. Return BusinessEntityID, quarter, year, differences between current quarter and first and last quarter. Sort the result set on BusinessEntityID, SalesYear, and Quarter in ascending order.

Sample table: Sales.SalesPersonQuotaHistory


Click to view Full table

Sample Solution:

SELECT BusinessEntityID
    , DATE_PART('quarter', QuotaDate) AS Quarter
    , date_part('year',QuotaDate) AS SalesYear
    , SalesQuota AS QuotaThisQuarter
    , SalesQuota - FIRST_VALUE(SalesQuota)
          OVER (PARTITION BY BusinessEntityID, date_part('year',QuotaDate)
              ORDER BY DATE_PART('quarter', QuotaDate)) AS DifferenceFromFirstQuarter
    , SalesQuota - LAST_VALUE(SalesQuota)
          OVER (PARTITION BY BusinessEntityID, date_part('year',QuotaDate)
              ORDER BY DATE_PART('quarter', QuotaDate)
              RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) AS DifferenceFromLastQuarter
FROM Sales.SalesPersonQuotaHistory
WHERE date_part('year',QuotaDate) > 2005 AND BusinessEntityID BETWEEN 274 AND 275
ORDER BY BusinessEntityID, SalesYear, Quarter;

Sample Output:

businessentityid|quarter|salesyear|quotathisquarter|differencefromfirstquarter|differencefromlastquarter|
----------------+-------+---------+----------------+--------------------------+-------------------------+
             274|    2.0|   2011.0|           28000|                         0|                   -63000|
             274|    3.0|   2011.0|            7000|                    -21000|                   -84000|
             274|    4.0|   2011.0|           91000|                     63000|                        0|
             274|    1.0|   2012.0|          140000|                         0|                    33000|
             274|    2.0|   2012.0|           70000|                    -70000|                   -37000|
             274|    3.0|   2012.0|          154000|                     14000|                    47000|
             274|    4.0|   2012.0|          107000|                    -33000|                        0|
             274|    1.0|   2013.0|           58000|                         0|                   -26000|
             274|    2.0|   2013.0|          263000|                    205000|                   179000|
             274|    3.0|   2013.0|          116000|                     58000|                    32000|
             274|    4.0|   2013.0|           84000|                     26000|                        0|
             274|    1.0|   2014.0|          187000|                         0|                        0|
             275|    2.0|   2011.0|          367000|                         0|                  -135000|
			 
...

SQL AdventureWorks Editor:

Practice Online


Contribute your code and comments through Disqus.

Previous: Fetch hiredate of last employee in a department for a salary.
Next: Find the statistical variance of the sales quota for each quarter.


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