Need some help in writing sql query to capture number of active Orders between date range on month wise grouping. i.e if the user selected 2018-01-01 to 2019-12-31, I have to show number of active orders on a month basis i.e total 12 records.
I'm querying against Order Table whose schema looks like below
OrderID CustomerFirstName PurchaseDate OrderEndDate
1 XYZ 2018-01-01 9999-12-31
2 ABC 2018-02-02 2018-06-30
3 PQR 2018-06-01 2018-06-30
4 GHI 2018-01-01 2018-02-28
Order EndDate 9999-12-31 is never ending order.All considers has existing order in all date ranges.
From My UX, if I select Jan to Dec... Results should
JAN ==> 2 orders Feb ==> 3 Orders => Order ID are 1,2,4.
Reason for Month FEB Order ID : 1,2,4 are consider as Active orders because their end dates are falling in FEB.
For example : ORDER ID : 1 having End date has 9999-12-31 which is never ending. Always Active orders in all the date range
Order ID : 2 having End Date has 2018-06-30 so till June he should be considered has Active order for every Month
Order ID : 4 having end date has 2018-02-28 for Feb month OrderID is active Orders
Expected Output
Month NoOfOrders
Jan 2
Feb 3
Month NoOfOrder
Jan 2
Feb 3