AdventureWorks Database: Sales by year using GROUP BY with an expression
SQL Query - AdventureWorks: Exercise-17 with Solution
17. From the following table write a query in SQL to retrieve the total sales for each year. Return the year part of order date and total due amount. Sort the result in ascending order on year part of order date.
Sample table: Sales.SalesOrderHeader
Sample Solution:
SELECT DATE_PART('year',OrderDate) AS "Year"
,SUM(TotalDue) AS "Order Amount"
FROM Sales.SalesOrderHeader
GROUP BY DATE_PART('year',OrderDate)
ORDER BY DATE_PART('year',OrderDate);
Sample Output:
Year |Order Amount | ------+-------------+ 2011.0|14155699.5250| 2012.0|37675700.3120| 2013.0|48965887.9632| 2014.0|22419498.3157|
SQL AdventureWorks Editor:
Practice Online
Contribute your code and comments through Disqus.
Previous: Count employees for each city group by using multiple tables.
Next: Specify which groups to include in the results.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- 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