Logic to get dynamic date range in query for Fiscal Year - sql

I have a requirement to select data for different date ranges, depending one the current month it is run on. I have a Stored Procedure that takes in a user supplied Date i.e. 04/06/2020 into the variable #PPE. Based on the Month portion of this date, I need to query back varying ranges as in a Fiscal Year (July 1 - June 30th) follows: I am using the example using a 2020 Financial Year.
MONTH(#PPE) Date Range to Query
July 07/01/2019 - 07/01/2019
August 07/01/2019 - 08/31/2019
September 07/01/2019 - 09/30/2019
October 07/01/2019 - 09/30/2019
November 07/01/2019 - 09/30/2019
December 07/01/2019 - 09/30/2019
January 07/01/2019 - 12/31/2019
February 07/01/2019 - 12/31/2019
March 07/01/2019 - 12/31/2019
April 07/01/2019 - 03/31/2020
May 07/01/2019 - 03/31/2020
June 07/01/2019 - 03/31/2020
Then Starting next Fiscal Year (2021) it would go as follows, and onwards...
MONTH(#PPE) Date Range to Query
July 07/01/2020 - 07/01/2020
August 07/01/2020 - 08/31/2020
September 07/01/2020 - 09/30/2020
October 07/01/2020 - 09/30/2020
November 07/01/2020 - 09/30/2020
December 07/01/2020 - 09/30/2020
January 07/01/2020 - 12/31/2020
February 07/01/2020 - 12/31/2020
March 07/01/2020 - 12/31/2020
April 07/01/2019 - 03/31/2021
May 07/01/2019 - 03/31/2021
June 07/01/2019 - 03/31/2021
I started with this query , but I'm having trouble manipulating the correct FISCAL_YEAR to put in, depending on what the month of the #PPE date is, because it spans 2 years between April and June.
PERIOD represents the Fiscal Month number i.e. July = 1 , August = 2, ....June = 12
DECLARE #PPE datetime; SET #PPE = '04/06/2020'
SELECT COMPANY, DEPTID, FISCAL_YEAR, PERIOD, GH_REPORTID, SUM(GH_HOURS), SUM(GH_DOLLARS)
FROM PS_GH_SLT_ACTUALS
WHERE DEPTID = 55650000
AND GH_REPORTID = 'BA'
AND PERIOD <= CASE WHEN MONTH(#PPE) BETWEEN 1 AND 3 THEN 12
WHEN MONTH(#PPE) BETWEEN 4 AND 6 THEN 3
WHEN MONTH(#PPE) BETWEEN 7 AND 9 THEN 6
WHEN MONTH(#PPE) BETWEEN 10 AND 12 THEN 9
END
-- AND PERIOD <= 7 ???
AND FISCAL_YEAR = CASE WHEN MONTH(#PPE) BETWEEN 1 AND 3 THEN YEAR(#PPE)
WHEN MONTH(#PPE) BETWEEN 4 AND 6 THEN ....???
GROUP BY COMPANY, DEPTID, FISCAL_YEAR, PERIOD, GH_REPORTID

If your fiscal year is already in the format, then you can use the following and condition:
FISCAL_YEAR = CASE WHEN MONTH(#PPE) BETWEEN 1 AND 6 THEN YEAR(#PPE)
ELSE YEAR(#PPE)+1 END
The above condition should work under the assumption that, Jul 2019 to Jun 2020 is called as Fiscal year 2020, as per your data. Hope this helps.

Related

Query Sales Group by week no and Month

I have to calculate sales based on WeekNo(Yearly) and Month.
Table1: Sales
ID SalDate Amount Region
1 2020-12-27 1000 USA
2 2020-12-28 1000 EU
3 2020-12-29 1000 AUS
4 2021-01-01 1000 USA
5 2021-01-02 1000 EU
6 2021-01-05 1000 AUS
7 2020-09-30 1000 EU
8 2020-10-01 1000 AUS
Select DateName(Month,SalDate)+' - '+Convert(Varchar(10),Year(SalDate)) Months,
'Week '+ Convert(Varchar(50), DATEPART(WEEK, SalDate)) As Weeks,
Sum(Amount) OrderValue
From [Sales]
Group By DateName(Month,SalDate)+' - '+Convert(Varchar(10),Year(SalDate)),
Convert(Varchar(50), DATEPART(WEEK, SalDate))
Months Weeks Total
January - 2021 Week 1 2000.00
January - 2021 Week 2 1000.00
December - 2020 Week 53 3000.00
October - 2020 Week 40 1000.00
September - 2020 Week 40 1000.00
But client want to merge Week1 Total with Week 53
And Week2 show as Week1.
And Week 40 Merge with Sep 2020.
I want result like below
Months Weeks Total
January - 2021 Week 1 1000.00
December - 2020 Week 53 5000.00 (2000+3000)
September - 2020 Week 40 2000.00 (1000+100)
Kindly help me to fix this problem.

How do we convert 2018-12-31 as Week 1 of 2019 in SQL?

Using CONCAT(TO_CHAR(T.order_create_date,'yyyy')," ",WEEKOFYEAR(T.order_create_date)) returns 2018-12-31 as 2018-1.
The first week of each year starts on the first Monday on or before January 4 of that year, so that the year begins from December 28 of the prior year through January 4 of the current year.
SELECT(DATEPART(dy,DATEADD(dd,DATEDIFF(dd,'17530101',#SomeDate)/7*7,'17530104'))+6)/7;

SQL Create a midweek week-ending date without using a joining lookup table

I'm wondering if it is possible to build a ORACLE SQL FUNCTION to return a mid-week date rather than using a joining table.
The week ends on a Wednesday so if we had date of Friday 5th Jan then the week ending date would be the following Wednesday 10/01/2018
MON 1ST JAN - 03/01/18
TUES 2ND JAN - 03/01/18
WED 3RD JAN - 03/01/18
THURS 4TH JAN - 10/01/18
FRI 5TH JAN - 10/01/18
SAT 6TH JAN - 10/01/18
SUN 7TH JAN - 10/01/18
MON 8TH JAN - 10/01/18
TUES 9TH JAN - 10/01/18
WED 10TH JAN - 10/01/18
THURS 11TH JAN - 17/01/18
The reason I'm investigating the function route is to future proof the process so I do not have maintain a second lookup table & get rid of unnecessary joins.
I've managed to google a work around
SELECT NEXT_DAY('25-JAN-2018 00.00', 'WEDNESDAY')FROM DUAL;
You may need something like the following:
select d,
case
when trunc(d, 'IW') +2 >= d
then trunc(d, 'IW') +2
else trunc(d, 'IW') +9
end as next_wed
from (
select date '2018-01-01' + level -1 as d
from dual
connect by level <= 11
)
This gets the monday of the week in which your date is contained and then adds 2 or 9 depending on the fact that adding 2 gives a day before the inut date or not.
Another way could be by checking if the input date is a before or after the wednesday in its week:
case when to_char(d, 'D') <= 3
then trunc(d, 'IW') +2
else trunc(d, 'IW') +9
end as next_wed

SQL - pick current date from machine and compare the year

Scenario 1: Current Year
Always code needs to pick
The last Sunday of January for the current year. For ex(31-01-2016)
Current code - picks 1st of Jan 2016
convert(date,DATEADD(yy, DATEDIFF(yy, 0, getdate()), 0))
Scenario 2: Last Year
Always code needs to pick
The last Sunday of January for the Previous year. For ex(01-02-2015)
Current code - picks 1st of Jan 2015
convert(date,DATEADD(yy, DATEDIFF(yy, 0, dateadd(YEAR, - 1, getdate())), 0))
Instead of hard coding the date. I would like to pick date from machine and compare.
Week start from Sunday and ends Saturday. Any help please?
-- Sample Demonstrative Data/Test Results
Declare #YourTable table (SomeDate date)
Insert Into #YourTable values ('2000-06-15'),('2001-06-15'),('2002-06-15'),('2003-06-15'),('2004-06-15'),('2005-06-15'),('2006-06-15')
,('2007-06-15'),('2008-06-15'),('2009-06-15'),('2011-06-15'),('2012-06-15'),('2013-06-15'),('2014-06-15'),('2015-06-15'),('2016-06-15')
,('2017-06-15'),('2018-06-15'),('2019-06-15'),('2020-06-15')
-- To Confirm Results
Select Year = year(SomeDate)
,LastSundayDate = DateAdd(DD,-DatePart(DW,DateFromParts(Year(SomeDate),12,31))+1,DateFromParts(Year(SomeDate),12,31))
,LastSundayName = DateName(DW,DateAdd(DD,-DatePart(DW,DateFromParts(Year(SomeDate),12,31))+1,DateFromParts(Year(SomeDate),12,31)))
From #YourTable
Returns
Year LastSundayDate LastSundayName
2000 2000-12-31 Sunday
2001 2001-12-30 Sunday
2002 2002-12-29 Sunday
2003 2003-12-28 Sunday
2004 2004-12-26 Sunday
2005 2005-12-25 Sunday
2006 2006-12-31 Sunday
2007 2007-12-30 Sunday
2008 2008-12-28 Sunday
2009 2009-12-27 Sunday
2011 2011-12-25 Sunday
2012 2012-12-30 Sunday
2013 2013-12-29 Sunday
2014 2014-12-28 Sunday
2015 2015-12-27 Sunday
2016 2016-12-25 Sunday
2017 2017-12-31 Sunday
2018 2018-12-30 Sunday
2019 2019-12-29 Sunday
2020 2020-12-27 Sunday

Select and group past data relative to a certain date

I have data in two tables ORDERS and training.
Different kinds of training are provided to various customers. I would like to see what was the affect of these training on revenue for the customers involved. For achieving this, I would like to look at the revenue for the past 90 days and the future 90 days for each customer from the date of receiving the training. In other words, if a customer received a training on March 30 2014, I would like to look at the revenue from Jan 1 2014 till June 30 2014. I have come up with the following query which pretty much does the job:
select
o.custno,
sum(ISNULL(a.revenue,0)) as Revenue,
o.Date as TrainingDate,
DATEADD(mm, DATEDIFF(mm, 0, a.created), 0) as RevenueMonth
from ORDERS a,
(select distinct custno, max(Date) as Date from Training group by custno) o
where a.custnum = o.custno
and a.created between DATEADD(day, -90, o.Date) and DATEADD(day, 90, o.Date)
group by o.custno, o.Date, DATEADD(mm, DATEDIFF(mm, 0, a.created), 0)
order by o.custno
The sample output of this query looks something like this:
custno Revenue TrainingDate RevenueMonth
0000100 159.20 2014-06-02 00:00:00.000 2014-03-01 00:00:00.000
0000100 199.00 2014-06-02 00:00:00.000 2014-04-01 00:00:00.000
0000100 79.60 2014-06-02 00:00:00.000 2014-05-01 00:00:00.000
0000100 29.85 2014-06-02 00:00:00.000 2014-06-01 00:00:00.000
0000100 79.60 2014-06-02 00:00:00.000 2014-07-01 00:00:00.000
0000100 99.50 2014-06-02 00:00:00.000 2014-08-01 00:00:00.000
0000250 437.65 2013-02-26 00:00:00.000 2012-11-01 00:00:00.000
0000250 4181.65 2013-02-26 00:00:00.000 2012-12-01 00:00:00.000
0000250 4146.80 2013-02-26 00:00:00.000 2013-01-01 00:00:00.000
0000250 6211.93 2013-02-26 00:00:00.000 2013-02-01 00:00:00.000
0000250 2199.72 2013-02-26 00:00:00.000 2013-03-01 00:00:00.000
0000250 4452.65 2013-02-26 00:00:00.000 2013-04-01 00:00:00.000
Desired output example:
If the training was provided on March 15 2014, for customer number 100, I’d want revenue data in the following format:
CustNo Revenue TrainingDate RevenueMonth
100 <Some revenue figure> March 15 2014 Dec 15 2013 – Jan 14 2014 (Past month 1)
100 <Some revenue figure> March 15 2014 Jan 15 2014 – Feb 14 2014 (Past month 2)
100 <Some revenue figure> March 15 2014 Feb 15 2014 – Mar 14 2014 (Past month 3)
100 <Some revenue figure> March 15 2014 Mar 15 2014 – Apr 14 2014 (Future month 1)
100 <Some revenue figure> March 15 2014 Apr 15 2014 – May 14 2014 (Future month 2)
100 <Some revenue figure> March 15 2014 May 15 2014 – Jun 14 2014 (Future month 3)
Here, the RevenueMonth column doesn’t need to be in this format as long as it has the data relative to the training date. The ‘past’ and ‘future’ month references in the braces are only to explain the output, they need not be present in the output.
My query gets the data and groups the data by month. I would like the months to be grouped relative to the training date. For example - If the training was given on March 15, I would like the past month to be from Feb 15 till March 14, my query doesn't do that. I believe a little tweak in this query might just achieve what I'm after.
Any help with the query would be highly appreciated.
Something along these lines may do what you want:
select
t.custno,
sum(ISNULL(o.revenue,0)) as Revenue,
t.maxDate as TrainingDate,
((DATEDIFF(day, TrainingDate, o.created) + 89) / 30) - 3 as DeltaMonth
from ORDERS o
join (select custno, max([Date]) as maxDate from Training group by custno) t
on o.custnum = t.custno
where o.created
between DATEADD(day, -89, t.maxDate) and DATEADD(day, 90, t.maxDate)
group by t.custno, t.maxDate, DeltaMonth
order by t.custno
The general strategy is to compute a difference in months (or 30-day periods, really) from the training date, and group by that. This version uses from 89 days before to 90 days after the training, because if you run from -90 to +90 then you have one more day (the training day itself) than divides evenly into 30-day periods.
The query follows the general structure of the original, but there are several changes:
it computes DeltaMonth (from -3 to 2) as an index of 30-day periods relative to the training date, with the training date being the last day of DeltaMonth number -1.
it uses the DeltaMonth alias in the GROUP BY clause instead of repeating its formula. That provides better clarity, simplicity, and maintainability.
I changed the table aliases. I simply could not handle there being a table named "ORDERS" and an alias "o", with the latter not being associated with the former.
the query uses modern join syntax, for better clarity
the GROUP BY clause updated appropriately