How to provide the dynamic date value in interval range between clause in analytic function - oracle11g

we provide the following
range between interval '1' day preceding and current row.
Can we provide any date range instead of '1' day?

Yes, but with an interval literal the '1' part can't be a variable; but you can multiply that by some variable:
range between some_number * interval '1' day preceding and current row
You can also use the numtodsinterval() function:
range between numtodsinterval(some_number, 'DAY') preceding and current row
or if you aren't dealing with just a single unit (i.e. a number of days) you can use the to_dsinterval() function:
range between to_dsinterval(some_string) preceding and current row
db<>fiddle
The first options can use any unit, of course; you can have multiple of 12 hours, or 3 seconds, or whatever you want.
The last option allows two formats, so you can pass in an ISO-format duration like 'P5D' or 'P1D12H' or 'P6D12H13M14.567S' etc., or an 'SQL format' as `6 12:13:14.567'.

Related

Year-month-week expression

I have a data written in specific expression. To simplify the data, here is the example I made:
df<-data.frame(date=c(2012034,2012044,2012051,2012063,2012074),
math=c(100,100,23,46,78))
2012034 means 4th week of march,2012. Likewise 2012044 means 4th week of April,2012. I was trying to make the values of date expressing some order. The reason why I have to do this is because when I don't change them to time expressions, x axis of the scatter plot looks really weird.
My goal is this:
Find the oldest date in date column and name it as 1. In this case, 2012034 should be 1. Next, find the second oldest date in date column and calculate how many weeks passed after that date. The second oldest date in date is 2012044.So, 5 weeks after the oldest date 2012034. So it should be changed as 1+5=6. So, likewise, I want to number the date to indicate how many weeks have passed since the oldest date
One way to do it is by also specifying the day of the week and subtract it at the end, i.e.
as.Date(paste0(df$date, '-1'), '%Y%m%U-%u') - 1
#[1] "2012-03-22" "2012-04-22" "2012-05-01" "2012-06-15" "2012-07-22"

Netsuite saved search formula that sums distinct values within a date range

I am trying to create a saved search of time entries in Netsuite.
Under criteria, I have specified a date range. This varies as this is an available filter
For the purposes of this example, the date range is 1/4/2020 to 10/4/2020
The first column ('Total Customer Billable Hours') in this sums all time entries that are coded against project task type 'billable project'. The formula I am using for this:
Formula (Numeric), sum, Case when {project.task_type}='Billable' then {durationdecimal} else 0 end
For the second column, I want the sum of hours the employee would normally work (in the time period specified under criteria-1/4/2020 to 10/4/2020 in this example)
The formula I am using to sum this is
Formula(numeric), sum, {timesheet.workcalendarhoursdecimal}
However, this is multiplying the employee's weekly hours by the number of time entries that make up the 'Total customer billable hours' figure
i.e. if an employee works a 40 hour week, the formula is multiple 40 x 36 (the number of time entries that make up the customer billable figure for example)
What would the correct formula be so that the second column is only summing the employee's work calendar hours for the period specified in the criteria/available filter selection?
Try changing sum to maximum:
Formula(numeric), maximum, {timesheet.workcalendarhoursdecimal}

Excel - How do I match a minute in time series with specified minute to return max value from another column?

I am trying to extract the max values of CO2ppm (column E) that were logged every second over 1 hour (column D) for a total of 60 minutes (rows ~3300). I have column D for time (in HH:MM:SS) and CO2ppm (numeric). I have 50 CO2 samples that I collected that correspond with a minute (e.g. I collected sample #1 at minute 20:54 in F2), but the data is logging every second within that minute, and I want the the highest CO2 ppm in that minute).
The =MAX(IF(D:D=A2,E:E)) works to return the max value CO2ppm when I use the target value as the date (A2) for the entire day of sampling, but it does not work when I try to match my target minute (F2, 20:54) with the column D (HH:MM:SS). I tried to convert this column to text using =TEXT(D:D,"H:M") so that the target value will match the values of minute, excluding all of the seconds, but with no luck.
How can I match my minute (F2) with the range of rows that have that minute (20:54:xx, column D) to find the max value in column E?
Example data:
Thank you!
An easy way to do this would be to add a helper column with the timestamp stripped of the second component.
However in case that is not an option, you could use a formula like the following, which strips out the seconds from the timestamps in column D:
=MAX(IF((D2:D5-SECOND(D2:D5)/86400)=F2,E2:E5))
Depending on your version of Excel, you may have to confirm the formula with Ctrl+Shift+Enter.

How do you update an existing date to a random date in a range?

In one of my tables I have datetime field in which the data in the table column is populated with something like "2016-01-07 01:33:00".
What I want to do is change ONLY the date to a random date within a range (ie: 2016-02-01 thru 2016-02-28) without changing the time. The end result might be "2016-02-13 01:33:00".
What mysql command string would accomplish this task?
Something like
UPDATE someTable SET someDate = DATE_ADD(
someDate,
INTERVAL
DATEDIFF(rangeStart, someDate) +
ROUND(RAND()*DATEDIFF(rangeEnd, rangeStart))
DAY
);
where someTable.someDate is your existing data, and rangeStart and rangeEnd are the boundaries of your target date range.
Here you take the initial date, add enough days to it to reach the range start, and then further add a random number of days no greater than the number of days in your target range.
In MsSQL it could be:
select dateadd(day,cast((RAND() * 30) as int),getdate())
Substitute getdate() with your input date.
(RAND() * 30) is used to randomly generate a number of days up to 30.

DateAdd function in pl/sql

As the title suggested , I am looking for a function in pl /sql which does something similar like the DateAdd function.
I have been looking and I found the add_months function but I would really like one that is a little more variable since I need to be able to add minutes, hours , days etc.
It appears there's not many solutions :
PL/SQL allows you to perform arithmetic operations directly on date variables. You may add numbers to a date or subtract numbers from a date. To move a date one day in the future, simply add 1 to the date as shown below:
hire_date + 1
You can even add a fractional value to a date. For example, adding 1/24 to a date adds an hour to the time component of that value. Adding 1/(24*60) adds a single minute to the time component, and so on.
Besides adding numbers to dates - though it's the simplest way - you can add intervals like that:
date1 := date2 + interval '1' day;
date1 := date2 + interval '2' month;
date1 := date2 + interval '3' year;
It's almost the same but I prefer latter for better readability.

Resources