Oracle: Date Range - sql

I have the below Query & table:
Problem:
I'm trying to capture IDs that were Reassigned for that month.
For example the month of August total number of ReAssigned would be 1 and not 3
How can this be captured?
I need to count Per Month, for example. How mamy ids were reassigned for the month of August, Sept, ect.
So, the Most_Rent_Assigment date needs to fall within the month
Aug: 8/1-8/31
Sept: 9/1-9/30
Then count to capture how many ids were reassigned in that month
,CASE WHEN
H.FIRST_ASSGN_DT IS NULL THEN 'UnAssigned'
WHEN h.TOTAL_NUMBER_OF_REASSIGNMENTS = 0 OR h.TOTAL_NUMBER_OF_REASSIGNMENTS IS NULL
AND h.FIRST_ASSGN_DT IS NOT NULL THEN 'NewlyAssigned'
ELSE 'ReAssigned'
END ASSIGNED_STATUS
Table:
ID Total_Number_Reassignment Most_Recent_Assignment StartDate *Assigned Status
1 2 11/01/2016 08/1/2017 ReAssigned
2 3 08/02/2017 08/01/2017 ReAssigned
3 0 08/15/2017 NewlyAssigned
4 5 12/01/2016 09/01/2017 ReAssigned

Date ranges in Oracle can be accomplished by using BETWEEN.
If the columns are type DATE then you can just do: WHERE TRUNC(Most_Recent_Assignment) BETWEEN TO_DATE('2017-08-01') AND TO_DATE('2017-08-31')
Note: the TRUNC will truncate the date to midnight, so you don't lose results based on time of day.
Edit:
OP clarified his question.
To count occurrences for a particular month, you can group by month.
SELECT
TO_CHAR('Most_Recent_Assignment', 'mm'),
COUNT(TO_CHAR('Most_Recent_Assignment', 'mm'))
FROM TABLE
WHERE ...
GROUP BY TO_CHAR('Most_Recent_Assignment', 'mm')
TO_CHAR parses a date as a string. mm will return a 2-digit month. You can replace it with Month to get the name of the month.
The group will put all months together.

Related

Calculate Week Numbers based on the initial given date to end date

I have below scenario that Business want to calculate Week Number based on Given Start Date to End Date.
For Ex: Start Date = 8/24/2020 End Date = 12/31/2020 ( These Start date & end date are not constant they may change from year to year )
Expected Output below:
[Date 1 Date 2 Week Number
8/24/2020 8/30/2020 week1
8/31/2020 9/6/2020 week2
9/7/2020 9/14/2020 week3
9/15/2020 9/21/2020 week4
9/22/2020 9/28/2020 week5
9/29/2020 10/5/2020 week6
10/6/2020 10/12/2020 week7
10/13/2020 10/19/2020 week8
10/20/2020 10/26/2020 week9
10/27/2020 11/02/2020 week10
11/03/2020 11/09/2020 week11
11/10/2020 11/16/2020 week12
11/17/2020 11/23/2020 week13
11/24/2020 11/30/2020 week14
I need Oracle Query to calculate Week Number(s) like above .. Based on Start date for 7 days then week number will be calcuated.. But remember that crossing months some month have 30 days and some month 31 days etc.. How to calculate ? Appreciate your help!!
Seems your looking for custom week definition rather that built-ins. But not overly difficult. The first thing is to convert from strings to dates (if columns actually coming off table this conversion is not required), and from there let Oracle do all the calculations as you can apply arithmetic operations to dates, except adding 2 dates. Oracle will automatically handle differing number of days per month correctly.
Two methods for this request:
Use a recursive CTE (with)
with dates(start_date,end_date) as
( select date '2020-08-24' start_date
, date '2020-12-31' end_date
from dual
)
, weeks (wk, wk_start, wk_end, e_date) as
( select 1, start_date, start_date+6 ld, end_date from dates
union all
select wk+1, wk_end+1, wk_end+7, e_date
from weeks
where wk_end<e_date
)
select wk, wk_start, wk_end from weeks;
Use Oracle connect by
with dates(start_date,end_date) as
( select date '2020-08-24' start_date
, date '2020-12-31' end_date
from dual
)
select level wk
, start_date+7*(level-1) wk_start
, start_date+6+7*(level-1)
from dates
connect by level <= ceil( (end_date-start_date)/7.0);
Depend on how strict you need to be with the end date specified you may need to adjust the last row returned. Both queries do not make adjust for that. They just ensure no week begins after that date. But the last week contains the full 7 days, which may end after the specified end date.
If your date datatype is varchar then first convert it to date and then convert it back to varchar.
convert date to to_char(to_date('8/24/2020','MM/DD/YYYY'),'WW')
If you to keep week datatype as a number then you can do something like this
to_number(to_char(to_date('8/24/2020','MM/DD/YYYY'),'WW'))
Few options according to your need.
WW Week of year (1-53) where week 1 starts on the first day of the year and continues to the seventh day of the year.
W Week of month (1-5) where week 1 starts on the first day of the month and ends on the seventh.
IW Week of year (1-52 or 1-53) based on the ISO standard.

Data of last 6 quarter including current quarter

How to get data of last 6 quarter in Oracle including current quarter. I mean if I run the query today so data between 01-JAN-2018 to 30-JUN-2019 should come in the query.
You could do something like this:
SELECT
*
FROM
DUAL
WHERE
DATE_FIELD >= (SYSDATE - (30*(3*6)))
What this query is doing is taking the current date (SYSDATE), and grabbing all values from the previous 6 quarters. The rationale is:
30 = days in a month | 3 = months in a quarter | 6 = quarters specified by OP
You can use add_months and trunc functions for date value with Q(quarter) argument
select t.*
from tab t
where insert_date between
trunc(add_months(sysdate,-3*5),'Q')
and trunc(add_months(sysdate,3),'Q')-1;
Demo for verification
for the starting date, -3*(6-1) = -3*5 considered, starting from 5 quarter back to be able to count 6 quarter including the current quarter. 3 is
obvious as being the number of months in each quarter.

Derive 445 Quarter for any given date

I need assistance writing a query that determines the quarter of a given date based on 445 FY calendar. The FY ends on first Friday of February every year.
For example in attached image the table has the order id and order created date. Based on the order created date I want to determine which 445 FY quarter it falls in.
Use a case statement with a between condition to divide up your custom quarters. You will need to enter the start and end dates of each quarter:
Select
Case
when date between 'startdateQ1' and 'enddateQ1' then 'Quarter1'
when date between 'startdateQ2' and 'enddateQ2' then 'Quarter2'
when date between 'startdateQ3' and 'enddateQ3' then 'Quarter3'
when date between 'startdateQ4' and 'enddateQ4' then 'Quarter4'
when date is null then 'Null'
else 'Unknown'
end
As Current_Quarter
From DB

Select next value where it equals another value

I have a table of the fiscal year for 100 years. i.e.
I am wanting to do add a column which shows the fiscal year that each week_ending_date belongs to. So in the table above week number 1, and week_ending_date 2013-10-05 would belong to fiscal year ending 2014.
In short I simply want each value in the added column to be the year part of week_ending_date where the next week_number is 52.
Here would be the pseudo code of what I am trying to achieve.
SELECT
Week_Ending_Date,
(SELECT NEXT VALUE FOR Week_Ending_Date (***but as only year***) FROM Fiscal_Calendar WHERE FC.Week_Number = 52)
FROM Fiscal_Calendar AS FC
JOIN Shipped_Container AS SC
ON SC.Fiscal_Week_Ending = FC.Week_Ending_Date
Bare in mind this has 100 years in the table and so I can't select the last value and makes using WHERE difficult (for me).
One way you can do this is by subtracting 7 * week number, taking the year and adding 1:
select 1 + year(dateadd(day, -7 * week_number, week_ending_date))

How to find if a date is from a Odd or Even week?

In my table I have for each day:
Id_______Startdate _______ EndDate __________MondayMorning _____MondayEvening ___TuMorning ....
121 _____2012-01-01________2012-12-31 ________2 ___________________2______________2
122 _____2012-02-01________2012-08-05 ________1 ___________________2______________3
I already generated a list of dates using the Startdate and EndDate.
I want to know if this dates belongs to a odd week or a even week so that I can filter days from my output that have number 3 Or 1 (see second record).
how can I filter the days that belongs to odd weeks and even weeks that have number 1 and 3?
You can use datepart with the wk argument to determine the week number:
SELECT datepart(wk, YourDate)
From there, you can use modulus to determine if the week number is even or odd:
SELECT datepart(wk, YourDate) % 2
This will return 0 for even numbered weeks, and 1 for odd numbered weeks.