Return date after each month cut off date in Presto SQL - sql

I have encountered this situation where I need to return a specific date for each cut off automatically. The cut off will be 5 days before each month.
Desire output :
Start from Jan 1, the calculation of transaction will be between 2023-01-01 and 2023-01-26
eg. from this period there are 100 transactions from 10 users.
Then the date will automatically start again between 2023-01-27 and 2023-02-23
eg. from this period there are 200 transactions from 20 users.
and so on..
So, basically the data will returns on each cut off
Any help would be appreciated!

Related

How to return only the first date of a sequence of dates?

I'm breaking my head trying to create a query for the following situation: I'm using an oracle database, I have a job that always runs at 00 o'clock, so I will fetch the beginning of a user's recess, in the example below we see how are the dates.
DATE_RECESS | USER_ID
---------------------------
22/09/21 | 1
21/09/21 | 1
20/09/21 | 1
19/09/21 | 1
18/09/21 | 1
I will need to notify him missing 10 days to the beginning of the recess, this notification will be sent only 1 time ...
So, looking at these dates with example, I should send 1 single notification on 08/09/21, I should not notify on the other days, only on the first day.
I can't send notification for every day, I should have some return just missing 10 days to start. Summarizing there's my doubt:
How to create a query, (which will be executed by a job that will run every day), and that does not bring result every day? Only when the first date is found?
For me to know what dates are missing 10 days I will take as base my current date, today + 10 days... if I find any date in this period then there I have a recess, but the return of the query should only bring something if it is date is the day 18/09/21, because there is beginning of the recess ... if today + 10 fall on the 19th I will bring nothing, because there will be the second day ...

How to find the leave balance month wise in teradata

I have sample data like this
EmployeeID Begin Date End Date Duration
168835 28/11/2017 28/11/2017 6
168835 31/10/2017 9/11/2017 32
and I want to find the leave taken by this employee month wise,so i have joined with calendar date and i want to summarise this data monthly.For e.g, he has taken leave from 31/10/2017 to 9/11/2017 for 32 hours and 6 hours in 28/11/2017. I want to exclude weekend from 31/10/2017 to 9/11/2017 period. I have used "day of week" flag to exclude the weekends, but now problem is when i roll up the data its giving me incorrect result. basically, i want to data like duration (32+6)=38 and this should be divided no of working days that is 9 in this case, so in oct month my data should be 4.22 and in nov month it should 33.77. can anyone please help me on this.Thanks in advance.

Sumif Last date value in powerpivot

I'm working with excel 2016 and have already loaded my data in PowerPivot
My data is as below (summarized):
Date Time Amount
9/20/2017 8:20 100
9/20/2017 9:20 200
9/20/2017 10:20 180
9/21/2017 9:00 50
9/22/2017 22:00 110
All I want is to create a measure so that for each date, I sum the last Amount value (indicated in bold); so in the example, the desired value would be 290.
Thanks in advance
You can calculate the latest Time by day, and then use this in a measure.
Find the maximum time of date:
mymaxtime=CALCULATE(MAX(myfact[Time]);filter(myfact;myfact[Date]=EARLIER(myfact[Date])))
You can then use this calculated column in a new measure:
mycalculation:=CALCULATE(sum(myfact[Amount]);filter(myfact;myfact[Time]=(myfact[mymaxtime])))
Screenshot of the result:

QV - Count days in a year in script

In the script I need a counter to give the same day (in different years) a number, and when a new year begin it will start from 1 again. So when I reload my script I want a table that look like this:
Date Number
01/01/2015 1
...... ...
10/30/2015 303
10/31/2015 304
11/01/2015 305
.... ...
12/31/2015 365
01/01/2016 1
01/02/2016 2
.... ....
How can I do this?
DayNumberOfYear(date[,firstmonth])
Returns the day number of the year according to a timestamp with the first millisecond of the first day of the year containing date. The function always uses years based on 366 days.
By specifying a firstmonth between 1 and 12 (1 if omitted), the beginning of the year may be moved forward to the first day of any month. If you e.g. want to work with a fiscal year starting March 1, specify firstmonth = 3.
You could try subtracting the Date from the beginning of the year of the Date
Date-makedate(year(Date),1,1)+1 as Number
You'll need a bit more maths if leap years are going to matter in you analysis.

Sum the differences between dates

I have 2 tables, master and detail, that both contain dates related to events. The first contain the master record's begin and end date. The second contains various gaps, that also have a beginning and an end, related to the master record and falling between its begin and end date.
I can successfully calculate the total number of days between the master record's start and end but, yet I'm failing to see how i can aggregate the sum of the "off days" in the details table and group them by month. With this i mean:
Master table
Start date (MM/DD/YYYY): 01/01/2015
End date (MM/DD/YYYY): 01/25/2015
Total number of days: 25
Details table
Start date (MM/DD/YYYY) | End date (MM/DD/YYYY) :
01/02/2015 | 01/05/2015
01/09/2015 | 01/15/2015
01/18/2015 | 01/19/2015
Total number of "off days": 13
The DB environment is Oracle 11g.
Can you help me?
Try this :
select sum(End_date-Start_date+1) from details_table;
The sum function will sum the total of all the dates, which should give you the 13 "off days" you wanted.
If you want to add the start_date/end_date conditions, you can do it like this.
select sum(End_date-Start_date+1) from details_table
where Start_date>=to_date('01/01/2015','mm/dd/yyyy')
and End_date<=to_date('01/25/2015','mm/dd/yyyy');