How can I get results grouped by every 2 years?
Table 1: ORDERS
ORDER_ID MemberID OrderDate
+----------+--------------+-----+
1 a 05-06-2016
2 b 06-06-2016
3 a 06-06-2017
4 b 07-06-2017
5 h 08-06-2017
6 a 02-06-2018
7 b 03-06-2018
8 a 05-06-2019
9 b 09-06-2019
Expected Output Results:
YEAR ORDERS_AMOUNT
+----------+--------------+
2016-2017 5
2018-2019 4
I'm using the following query, but obvs it's not working...
SELECT year( o.orderDate) as Year,
count(o.order_id) as Order_Count
FROM orders o
Group by o.orderDate
Use the Group By of Year(orderDate)/2
SELECT (CONVERT(VARCHAR,MIN(year(OrderDate)))+'-' + CONVERT(VARCHAR,MAX(year(OrderDate)))) AS [Year],
COUNT(*) as [ORDERS_AMOUNT]
FROM Orders
GROUP BY FLOOR(Year(OrderDate) /2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With