Fiscal Month Start Dates - plsql

I have a fiscal Month End date column. Based on this column I will have to calculate 12 month prior fiscal start date, 24 month prior fiscal start date and so on.
I cannot use add_months function here as below are the fscl month end dates. When a user chooses 01-JUL-2017, the 12 months prior date should start at 03-JUL-16.
If I use add months the date range would change to be 01-JUL-2016 to 01-JUL-2017 but I need it to be 03-JUL-2017 to 01-JUL-2017.
FISCAL MONTH END DATES
30-JAN-16
27-FEB-16
02-APR-16
30-APR-16
28-MAY-16
02-JUL-16
30-JUL-16
27-AUG-16
01-OCT-16
29-OCT-16
26-NOV-16
28-JAN-17
25-FEB-17
01-APR-17
29-APR-17
27-MAY-17
01-JUL-17

Considering that your table has all the dates in the sequence, You can use the LAG function with parameter 11 (i.e. 11th previous date in the sequence).Take the user input from the where clause as a bind Variable.
( WHERE TO_CHAR(DT,'DD-MON-YY') = :curr_fiscal_end_dt )
SELECT Prev_fisc_start_date FROM
(
SELECT DT, LAG(DT, 11 ) OVER ( ORDER BY DT ) + 1 Prev_fisc_start_dt FROM
Your_table
) WHERE TO_CHAR(DT,'DD-MON-YY') ='01-JUL-2017';

Related

Impala - Working hours between two dates in impala

I have two time stamps #starttimestamp and #endtimestamp. How to calculate number of working hours between these two
Working hours is defined below:
Mon- Thursday (9:00-17:00)
Friday (9:00-13:00)
Have to work in impala
think i found a better solution.
we will create a series of numbers using a large table. You can get a time dimension type table too. Make it doenst get truncated. I am using a large table from my db.
Use this series to generate a date range between start and end date.
date_add (t.start_date,rs.uniqueid) -- create range of dates
join (select row_number() over ( order by mycol) as uniqueid -- create range of unique ids
from largetab) rs
where end_date >=date_add (t.start_date,rs.uniqueid)
Then we will calculate total hour difference between the timestamp using unix timestamp considering date and time.
unix_timestamp(endtimestamp - starttimestamp )
Exclude non working hours like 16hours on M-T, 20hours on F, 24hours on S-S.
case when dayofweek ( dday) in (1,7) then 24
when dayofweek ( dday) =5 then 20
else 16 end as non work hours
Here is complete SQL.
select
end_date, start_date,
diff_in_hr - sum(case when dayofweek ( dday) in (1,7) then 24
when dayofweek ( dday) =5 then 20
else 16 end ) total_workhrs
from (
select (unix_timestamp(end_date)- unix_timestamp(start_date))/3600 as diff_in_hr , end_date, start_date,date_add (t.start_date,rs.uniqueid) as dDay
from tdate t
join (select row_number() over ( order by mycol) as uniqueid from largetab) rs
where end_date >=date_add (t.start_date,rs.uniqueid)
)rs2
group by 1,2,diff_in_hr

How to get first date and last date from given month name and Year

In my page I have on dropDown ddMonth and ddYear ...ddMonth shows jan,Feb,mar etc. ddYear as 2014,2015... . By using this How can I get the first date and last date from this 2 dropdown (ddmonth and ddYear) by using asp.net.
The first date of always starts with 1 so you can create date using day as 1
DateTime dt = new DateTime(year, month, 1);
For last date of month you can add 1 to month and subtract on day from it.
DateTime dt = (new DateTime(year, month, 1).AddMonths(1)).AddDays(-1);

T-SQL - Determine number of specific days between 2 dates based on sales

The Problem: Given a day of the week (1, 2, 3, 4, 5, 6, 7), a starting date and an ending date, compute the number of times the given day of the week appears between the starting and ending dates not inclusive of a date for which there were no sales.
Context:
Table "Ticket" has the following structure and sample content:
i_ticket_id c_items_total dt_create_time dt_close_time
----------------------------------------------------------------------------
1 8.50 '10/1/2012 10:23:00' '10/1/2012 11:05:05'
2 10.50 '10/1/2012 11:00:00' '10/1/2012 11:45:05'
3 8.50 '10/2/2012 08:00:00' '10/2/2012 09:25:05'
4 8.50 '10/4/2012 08:00:00' '10/4/2012 09:25:05'
5 7.50 '10/5/2012 13:22:23' '10/5/2012 14:33:27'
.
.
233 6.75 '10/31/2012 23:20:00' '10/31/2012 23:55:39'
Details
There may or may not be any tickets for one or more days during a month. (i.e. the place was closed that/those day/s)
Days in which the business is closed are not regular. There is no predictable pattern.
Based on Get number of weekdays (Sundays, Mondays, Tuesdays) between two dates SQL,
I have derived a query which returns the number of times a given day of the week occurs between the start date and the end date:
DECLARE #dtStart DATETIME = '10/1/2013 04:00:00'
DECLARE #dtEnd DATETIME = '11/1/2013 03:59:00'
DECLARE #day_number INTEGER = 1
DECLARE #numdays INTEGER
SET #numdays = (SELECT 1 + DATEDIFF(wk, #dtStart, #dtEnd)-
CASE WHEN DATEPART(weekday, #dtStart) #day_number THEN 1 ELSE 0 END -
CASE WHEN DATEPART(weekday, #dtEnd) <= #day_number THEN 1 ELSE 0 END)
Now I just need to filter this so that any zero-dollar days are not included in the count. Any help you can provide to add this filter based on the contents of the tickets table is greatly appreciated!
If I understand correctly, you can use a calendar table to count the number of days where the day of week is n and between the start and end and is a date that has ticket sales, which I guess is when the date exists in tickets and has the sum(c_items_total) > 0
WITH cal AS
(
SELECT cast('2012-01-01' AS DATE) dt, datepart(weekday, '2012-01-01') dow
UNION ALL
SELECT dateadd(day, 1, dt), datepart(weekday, dateadd(day, 1, dt))
FROM cal
WHERE dt < getdate()
)
SELECT COUNT(1)
FROM cal
WHERE dow = 5
AND dt BETWEEN '2012-04-01' AND '2012-12-31'
AND EXISTS (
SELECT 1
FROM tickets
WHERE cast(dt_create_time AS DATE) = dt
GROUP BY cast(dt_create_time AS DATE)
HAVING sum(c_items_total) > 0
)
OPTION (MAXRECURSION 0)
SQLFiddle

SQL Calculation for ASP.NET

In my SQL database I have a column that contains a fiscal year value. Fiscal Year start from Oct 1st to Sept 30th of the following year. For example the current fiscal year is 2011-2012 or "2011". I need a calculation to pull dates from my database.
I have a column in my table that contains dates (i.e. 05/04/2012), I need a calculation that will pull the dates for the selected fiscal year? So when I need to see the data for the date 02/14/2003, then I would need the fiscal year of 2002.
This all ties into my ASP.NET page, where a user selects which fiscal year they want to view and a gridview appears with the information requested. So, if I choose fiscal year 2010, the data pulled into the gridview should be all records from 10/01/2010 to 09/30/2011. Thanks for the help, I have yet to try anything because I am not sure where to begin (sql newbie).
You can find the fiscal year by adding three months:
year(dateadd(month,3,'2011-09-30'))
So to select all entries in Fiscal year 2011, try:
select *
from YourTable as yt
where year(dateadd(month,3,yt.TransactionDt)) = 2011
I assume you are using SqlParameters to send data to the SQL Server. Then:
int fiscal_year = 2002;
SqlParameter fyStart = new SqlParameter("#FiscalYearStart",
SqlDbType.DateTime);
fyStart.Value = new SqlDateTime(fiscalYear, 10, 01);
SqlParameter fyEnd = new SqlParameter("#FiscalYearEnd",
SqlDbType.DateTime);
fyEnd.Value = new SqlDateTime(fiscalYear+1, 10, 01);
Then you can pass these two params to an Stored Procedure for example, and to query the table with
WHERE date BETWEEN #FiscalYearStart AND #FiscalYearEnd
N.B. FiscalYearEnd should be 10-Oct-YEAR+1, as it will be represented as YYYY-10-01T00:00:00, or will include the whole 30 Sept.
You could query for it:
SELECT *
FROM YourTable
WHERE YourTableDate >= CAST(#FiscalYear AS CHAR(4)) + '-10-01'
AND YourTableDate < CAST(#FiscalYear + 1 AS CHAR(4)) + '-10-01';
or, if you need the flexibility, you could alternatively have a table of date ranges which join to fiscal years. This gives you the ability to have multiple fiscal year definitions for, say, multiple tenants or companies, or allows the definition to change without changing your queries. You could then join to this table as needed to filter your results.
CompanyID FiscalYear StartDate EndDate
1 2010 2010-10-01 2011-09-30
1 2011 2011-10-01 2012-09-30
2 2010 2010-01-01 2011-12-31
2 2011 2011-01-01 2012-12-31
SELECT *
FROM YourTable t
INNER JOIN FiscalYear y
ON y.FiscalYear = t.FiscalYear
WHERE t.YourTableDate >= y.StartDate AND t.YourTableDate < DATEADD(d, 1, y.EndDate);

oracle pl/sql: datepart () function for weekend exclusion

can i use to_number(to_char()) function in order to exclude all the weekends from a range of dates?
For instance, there are two date columns in my table such as start and finish (in the form of '06/06/2011 10:00:00 am'), and i want to estimate the duration of finish-start excluding Saturdays and Sundays.
If I understand you right you want to calculate the difference between two dates, but exclude the 2 days of each weekend in the range from the result. Is that correct?
If this is what you want the below code should work with the following assumptions:
I am assume start and end will not be on weekends.
I am not validating that end is before start.
Basically its just a matter of working out how many weekends are in the date range. So obviously there's one weekend per 7 days. Then we just have to check if the range wraps around a weekend, and if so add one more.
FUNCION dateDiff( dt_start DATE, dt_end DATE ) RETURN NUMBER
IS
raw_diff NUMBER;
weekends NUMBER;
BEGIN
raw_diff := dt_end - dt_start;
weekends := TRUNC( raw_diff / 7 );
IF( ( dt_start - TRUNC( dt_start, 'DAY' ) )
> ( dt_end - TRUNC( dt_end , 'DAY' ) ) )
THEN
weekends := weekends + 1;
END IF;
RETURN raw_diff - ( weekends * 2 );
END;

Resources