Hello I am thirst to get help as I am stuck for two days on a complex logical query,if anybody can help to solve.
Order Table
id | region_id | created_at | sale
=============|=============|=========================
1 | 1 | 2011-09-21 | $250
2 | 2 | 2012-03-12 | $320
3 | 1 | 2010-09-15 | $300
4 | 2 | 2011-08-18 | $180
5 | 1 | 2012-04-13 | $130
6 | 3 | 2010-06-22 | $360
7 | 2 | 2011-09-25 | $330
Regions Table
id | region_name
=============|=============
1 | Region 1
2 | Region 2
3 | Region 3
Expected Output
What I have tried to achieve
select distinct `regions`.`region_name`, sum(orders.sale) as sum,
CASE WHEN MONTH(orders.created_at)>=4 THEN
concat(YEAR(orders.created_at), '-',YEAR(orders.created_at)+1)
ELSE concat(YEAR(orders.created_at)-1,'-', YEAR(orders.created_at))
END AS financial_year from `orders` inner join `regions` on `orders`.`region_id` = `regions`.`id` group by YEAR(orders.created_at), `regions`.`region_name` order by `orders`.`region_id` asc, YEAR(orders.created_at) asc
My Queries Output
Where is my logical problem in query,one thing data should be fetched financial year wise not only normal year wise.
Thanks