My Financial year starts from September.
I need to select Fiscal day number, Fiscal week number, Fiscal Month number, Fiscal Quarter number, Fiscal Quarter Name
I am getting date range from below query :
select date_sub(boms, interval 0 day) as start_date
from unnest(generate_date_array('2020-01-01','2050-12-31', interval 1 day)) boms
Example :
2020-01-01
2020-01-02
2020-01-03
.....
.....
2025-12-31
The output i need to get :
For Example for the date : 11-Feb-2020
Output:
FISCAL_DAY_OF_YEAR_NUMBER- 162
FISCAL_WEEK_OF_YEAR_NUMBER- 24
FISCAL_MONTH_NUMBER - 6
FISCAL_QUARTER_NUMBER - 2
FISCAL_QUARTER_NAME - Third
Related
I am using SQL Server 2014 and I have a table in my Database called Date Dimension.
It has a column called "Date" which contains daily dates from 01 January 2013 to 31 December 2025 (extract of the column given below):
Date
2022-01-01
2022-01-02
2022-01-03
2022-01-04
2022-01-05
2022-01-06
2022-01-07
2022-01-09
...
2022-01-30
2022-01-31
...
I need to create a new column in the Date Dimension Table called "HR_WeekGroup" based on the following logic:
A WeekGroup will start on a Monday and end on a Sunday. WeekGroups will be calculated for EACH Month separately. That is, if the 1st Day of a Month is a Saturday, the WeekGroup for that week will consist of only 2 days (Saturday and Sunday) and if the last Day of that Month is a Monday, the weekGroup will consist of only 1 Day (Monday).
Here is what I'm after (based on the Month of January 2022):
Date HR_WeekGroup
2022-01-01 Wk 01Jan2022-02Jan2022
2022-01-02 Wk 01Jan2022-02Jan2022
2022-01-03 Wk 03Jan2022-09Jan2022
2022-01-04 Wk 03Jan2022-09Jan2022
2022-01-05 Wk 03Jan2022-09Jan2022
2022-01-06 Wk 03Jan2022-09Jan2022
2022-01-07 Wk 03Jan2022-09Jan2022
2022-01-09 Wk 03Jan2022-09Jan2022
... ...
2022-01-30 Wk 24Jan2022-30Jan2022
2022-01-31 Wk 31Jan2022
...
What would be the T-SQL Code that would allow me to create this column?
I had a look at the following pages but I can't figure out how to apply the examples to my specific problem.
What is the SQL syntax to create a column in my Date Dimension Table that will group the dates into this specific week grouping?
How to Group Data by Week in SQL Server
Set DateFirst 1;
-- The above changes the start of the week to Monday.
-- This makes the week as starting on Monday and ending on Sunday
-- DayOfWeek 1 means Monday
-- DayOfWeek 7 means Sunday
with cte as (
select
Date_Column,
MONTH(Date_Column) as [MONTH], -- Month in the year
DAY(Date_Column) as [DAY], -- Day in the month
DATEPART(week,Date_Column) as [WeekOfTheYear], --Week number of the year
DATEPART(dw,Date_Column) as [DayOfWeek], -- The week day of the week
Min(Date_Column) OVER (Partition by DATEPART(week,Date_Column), MONTH(Date_Column), YEAR(Date_Column) ) as [MinDateWeekGroup],
Max(Date_Column) OVER (Partition by DATEPART(week,Date_Column), MONTH(Date_Column), YEAR(Date_Column) ) as [MaxDateWeekGroup]
from date_dimension
--order by YEAR(Date_Column), MONTH(Date_Column), DAY(Date_Column)
)
select
Date_Column,
case when [MinDateWeekGroup] = [MaxDateWeekGroup]
THEN CONCAT('Wk ',
Right('0'+Convert(varchar(10), Day([MinDateWeekGroup])),2),
Convert(char(3), [MinDateWeekGroup], 0),Convert(char(4),YEAR([MinDateWeekGroup])) )
ELSE
CONCAT('Wk ',
Right('0'+Convert(varchar(10), Day([MinDateWeekGroup])),2),
Convert(char(3), [MinDateWeekGroup], 0),Convert(char(4),YEAR([MinDateWeekGroup])),
'-',
Right('0'+Convert(varchar(10), Day([MaxDateWeekGroup])),2),
Convert(char(3), [MaxDateWeekGroup], 0),Convert(char(4),YEAR([MaxDateWeekGroup]))
)
END as [HR_WeekGroup]
from cte
order by YEAR(Date_Column), MONTH(Date_Column), DAY(Date_Column)
Pre Condition: [I am using Week as Sunday as first day of week]
I have table '#TT' as below
Column -> D_Date
----------
2020-12-27 |
2020-12-28 |
2020-12-29 |
2020-12-30 |
2020-12-31 |
2021-01-01 |
2021-01-02 |
I want to get week numbers for date And in case of year end with new year start, I want to mark the first week as 0 for new year if it's days contribution is less than 4 days i.e.
As per US week
2020-12-27 from this **53rd** start
2020-12-28
2020-12-29
2020-12-30
2020-12-31
2021-01-01
2021-01-02 and end at here
In above from 27 till 31 are 5 days of 2020 whereas for 1 and 2 of 2021 are 2 days, thus majority days are from 2020 and not 2021.
Now I want to mark week number for 2021 as 0 for dates 1 & 2 and not 1
I am using below query but it is decreasing my minimum year's week number too. How to achieve this? Please help me.
SELECT
DATEPART( week, D_Date ),
CASE
WHEN COUNT( DATEPART( week, D_Date ) ) > 1 THEN MAX( DATEPART(week, D_Date ) - 1 )
ELSE DATEPART( week, D_Date )
END AS [State]
FROM
#tt
GROUP BY
DATEPART( year, D_Date ),
DATEPART( week, D_Date );
I have a table with the following rows:
date
2018-09-01
2018-10-01
2019-01-01
2019-09-01
2020-03-01
2020-08-01
Let's say fiscal year starts in October of the previous year and ends in September of that year. So fiscal year 2020 would be from 2019-10-01 to 2020-09-30. I am trying to count the dates that fall within each fiscal year so create the following table:
date_count fiscal_year
1 FY18
3 FY19
2 FY20
I've tried the following to get a single year:
SELECT count(*) as date_count, 'FY18' as fiscal_year
FROM table_name
WHERE date between '2017-10-01' and '2018-09-30'
and I get
date_count fiscal_year
1 FY18
But I need to do this for every fiscal year. I've seen similar answers online but none that has the date counts. Would I need to use a recursive query?
Just add 3 months and extract the year:
select t.*,
year(dateadd(month, 3, date)) as fiscal_year
from t;
I have two tables below. I want to count the number of days, Monday-Friday only between Hire_dt and end of calendar month the hire date falls under.
TableA
Hire_DT Id
09/26/2018 1
TableCalendar:
Date WorkDay(M-F) EOM WorkDay
09/26/2018 Wednesday 9/30/2018 1
09/27/2018 Thursday 09/30/2018 1
09/28/2018 Friday 09/30/2018 1
09/29/2018 Saturday 09/30/2018 0
09/30/2018 Sunday 09/30/2018 0
Expected Results
Hire_dt WorkDaysEndMonth WorkDaysEndMonth --counting hire_dt
09/26/2018 2 3
Here is one way to do the calculation - WITHOUT using a calendar table. The only input data is what comes from your first table (ID and HIRE_DATE), which I included in a WITH clause (not part of the query that answers your question!). Everything else is calculated. I show how to compute the number of days INCLUDING the hire date; if you don't need that, subtract 1 at the end.
TRUNC(<date>, 'iw') is the Monday of the week of <date>. The query computes how many days are in the EOM week, between Monday and EOM, but no more than 5 (in case EOM may be a Saturday or Sunday). It does a similar calculation for HIRE_DATE, but it counts the days from Monday to HIRE_DATE excluding HIRE_DATE. The last part is adding 5 days for each full week between the Monday of HIRE_DATE and the Monday of EOM.
with
sample_data(id, hire_date) as (
select 1, to_date('09/26/2018', 'mm/dd/yyyy') from dual union all
select 2, to_date('07/10/2018', 'mm/dd/yyyy') from dual
)
select id, to_char(hire_date, 'Dy mm/dd/yyyy') as hire_date,
to_char(eom, 'Dy mm/dd/yyyy') as eom,
least(5, eom - eom_mon + 1) - least(5, hire_date - hire_mon)
+ (eom_mon - hire_mon) * 5 / 7 as workdays
from (
select id, hire_date, last_day(hire_date) as eom,
trunc(hire_date, 'iw') as hire_mon,
trunc(last_day(hire_date), 'iw') as eom_mon
from sample_data
)
;
ID HIRE_DATE EOM WORKDAYS
---------- ----------------------- ----------------------- ----------
1 Wed 09/26/2018 Sun 09/30/2018 3
2 Tue 07/10/2018 Tue 07/31/2018 16
You may use the following routine ( where last_day function is a great contributor ):
SQL> alter session set NLS_TERRITORY="AMERICA";
SQL> create table TableA( ID int, Hire_DT date );
SQL> insert into TableA values(1,date'2018-09-26');
SQL> select sum(case when mod(to_char(myDate,'D'),7) <= 1 then 0 else 1 end )
as "WorkDaysEndMonth"
from
(
select Hire_DT + level - 1 myDate
from TableA
where ID = 1
connect by level <= last_day(Hire_DT) - Hire_DT + 1
);
WorkDaysEndMonth
----------------
3
P.S. integer value comes from to_char(<date>,'D') depends on the NLS_TERRITORY setting. Here I used AMERICA for which Saturday is the 7th and Sunday is the 1st day, while for UNITED KINGDOM(or my country TURKEY) setting those are 6th and 7th respectively.
Rextester Demo
I need a query to calculate a hotels income for every Thursday in a month but the total SUM has to be calculated for all the period between last weeks Friday and the next weeks Thursday.
Week 1 Friday -> Thursday Total income
Week 2 Friday -> Thursday Total income
and so on..
To explain it as simple as possible, I use the table RESERVATIONS to track the check-in and check-out date, number of guests and room type which is joined with the ROOM_PRICES table to calculate the prices.. How to calculate the SUM is not the problem, but to get that span between days is.
RESERVATIONS (reservation_ID, reservation_date, room_number,guest_id, number_of_guests, room_type, check_out_date)
ROOM_PRICE (room_type, price)
Expected output are two columns, Period and Total Income.
Total Income is the sum of all payments made by guests who spent their time in the hotel AND left on a week in between Friday and Thursday no matter if they spent two, three weeks in the hotel
Period |Total Income
|-------------------------------------|
|05.May.2017-11.May.2017 | 20000|
|-------------------------------------|
|12.May.2017-18.May.2017 | 30000|
|-------------------------------------|
|19.May.2017-25.May.2017 | 12000|
And now the list continues..
Any suggestions?
Thank you
Oracle Setup:
CREATE TABLE ROOM_PRICES(
hotel_id INTEGER,
day DATE CHECK ( day = TRUNC( day ) ),
price NUMBER(8,2),
CONSTRAINT room_prices__hotel_id__day__pk PRIMARY KEY ( hotel_id, day )
);
CREATE TABLE RESERVATIONS(
hotel_id INTEGER,
check_in DATE CHECK ( check_in = TRUNC( check_in ) ),
check_out DATE CHECK ( check_out = TRUNC( check_out ) ),
customer_id INTEGER
);
Query:
Subtract 4 from the date (so Friday maps to Monday) and then truncate to the iso-week (rounding to the Monday at the start of the week) then add the 4 days back on and add another 6 days to get the Thursday:
SELECT TRUNC( day - 4, 'IW' ) + 10 AS Thursday_of_week,
r.hotel_id,
SUM( price ) AS
FROM room_prices p
INNER JOIN reservations r
ON ( r.hotel_id = p.hotel_id
AND p.day >= r.check_in
AND p.day < r.check_out )
GROUP BY
r.hotel_id,
TRUNC( day - 4, 'IW' ) + 10