GROUP BY
with an expression exampleThe following example groups the orders by year and returns the number of orders per year.
SELECT EXTRACT ( YEAR FROM order_date) YEAR , COUNT ( order_id ) FROM orders GROUP BY EXTRACT ( YEAR FROM order_date) ORDER BY YEAR ; |
In this example, we used the EXTRACT()
function to get the year information from the order’s dates.
Unlike the previous examples, we used an expression that returns the year in the GROUP BY
clause.
The following picture illustrates the result:
GROUP BY
with an expression exampleThe following example groups the orders by year and returns the number of orders per year.
SELECT EXTRACT ( YEAR FROM order_date) YEAR , COUNT ( order_id ) FROM orders GROUP BY EXTRACT ( YEAR FROM order_date) ORDER BY YEAR ; |
In this example, we used the EXTRACT()
function to get the year information from the order’s dates.
Unlike the previous examples, we used an expression that returns the year in the GROUP BY
clause.
The following picture illustrates the result: