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
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.
SQL: Tips of the Day
Difference between natural join and inner join
One significant difference between INNER JOIN and NATURAL JOIN is the number of columns returned-
Consider:
TableA TableB +------------+----------+ +--------------------+ |Column1 | Column2 | |Column1 | Column3 | +-----------------------+ +--------------------+ | 1 | 2 | | 1 | 3 | +------------+----------+ +---------+----------+
The INNER JOIN of TableA and TableB on Column1 will return
SELECT * FROM TableA AS a INNER JOIN TableB AS b USING (Column1); SELECT * FROM TableA AS a INNER JOIN TableB AS b ON a.Column1 = b.Column1;
+------------+-----------+---------------------+ | a.Column1 | a.Column2 | b.Column1| b.Column3| +------------------------+---------------------+ | 1 | 2 | 1 | 3 | +------------+-----------+----------+----------+
The NATURAL JOIN of TableA and TableB on Column1 will return:
SELECT * FROM TableA NATURAL JOIN TableB +------------+----------+----------+ |Column1 | Column2 | Column3 | +-----------------------+----------+ | 1 | 2 | 3 | +------------+----------+----------+
Ref: https://bit.ly/3AG5CId
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
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