How to find difference between two timestamps in Talend HI - etl

I am new to Talend I want to find the difference between the two timestamps.
I am having two columns start_time and end_time.
I want to make a table in destination that will show the difference in both the timestamps, specifically I want to show hours mins and seconds.
Also I want time in timestamp not in ling format, how can I achieve this
start_time- 2021-06-18 08:27:52.000000
end_time- 2021-06-18 08:29:59.000000
I tried-
creating a variable 'ms' of long type in tmap = TalendDate.diffDate(row181.start_time,row181.end_time,"mm")
for converting into hh:mm:ss
String.format("%02d:%02d:%02d.%d", (Var.ms / (1000 * 60 * 60)) % 24, (Var.ms / (1000 * 60)) % 60, (Var.ms / 1000) % 60, Var.ms % 1000)
if I make table as string I am getting this err-
column "call_duration" is of type bigint but expression is of type character varying
Above T-map expression returning zero also I have to use long in the destination column type, but I want date type

Pattern "MM" refers to months, not minutes. Use "mm" instead.
How could you return a date type for a difference between two dates ? The result is necessarily a number (long/double...) .
If you want your output with hours/mins/seconds, you should use diffDate with "ss" pattern to get a long representing the duration in seconds. Then you'll have to transform this to get hours and minutes (e.g 3700 s would give you 1 hour, 1 minute, 40 seconds) . You also have to determine what kind of output you want (one column for each, a string with the concatenation of hours/minutes/seconds...)
Example : with row1.diffDate being your diffdate in seconds in input of a tMap, you could separate in three different columns. Then you'll only have to concatenate all values in a string. if you want a string output with ":" separator.

Related

SSAS Tabular - how to aggregate differently at month grain?

In my cube, I have several measures at the day grain that I'd like to sum at the day grain but average (or take latest) at the month grain or year grain.
Example:
We have a Fact table with Date and number of active subscribers in that day (aka PMC). This is snapshotted per day.
dt
SubscriberCnt
1/1/22
50
1/2/22
55
This works great at the day level. At the month level, we don't want to sum these two values (count = 105) because it doesn't make sense and not accurate.
when someone is looking at month grain, it should look like this - take the latest for the month. (we may change this to do an average instead, management is still deciding)
option 1 - Take latest
Month-Dt
Subscribers
Jan-2022
55
Feb-2022
-
option 2 - Take aveage
Month-Dt
Subscribers
Jan-2022
52
Feb-2022
-
I've not been able to find the right search terms for this but this seems like a common problem.
I added some sample data at the end of a month for testing:
dt
SubscriberCnt
12/30/21
46
12/31/21
48
This formula uses LASTNONBLANKVALUE, which sorts by the first column and provides the latest value that is not blank:
Monthly Subscriber Count = LASTNONBLANKVALUE( 'Table'[dt], SUM('Table'[SubscriberCnt]) )
If you do an AVERAGE, a simple AVERAGE formula will work. If you want an average just for the current month, then try this:
Current Subscriber Count =
VAR _EOM = CLOSINGBALANCEMONTH( SUM('Table'[SubscriberCnt]), DateDim[Date] )
RETURN IF(_EOM <> 0, _EOM, AVERAGE('Table'[SubscriberCnt]) )
But the total row will be misleading, so I would add this so the total row is the latest number:
Current Subscriber Count =
VAR _EOM = CLOSINGBALANCEMONTH( SUM('Table'[SubscriberCnt]), DateDim[Date] ) //Get the number on the last day of the month
VAR _TOT = NOT HASONEVALUE(DateDim[MonthNo]) // Check if this is a total row (more than one month value)
RETURN IF(_TOT, [Monthly Subscriber Count], // For total rows, use the latest nonblank value
IF(_EOM <> 0, _EOM, AVERAGE('Table'[SubscriberCnt]) ) // For month rows, use final day if available, else use the average
)

Time duration in Google Data Studio

I have some data I collect regarding length of time that's stored in HH:MM format. The data is in relation to sleep patterns (i.e. sleep duration, time fell asleep, etc...).
I am trying to import the data in Google Data Studio (DS) as a numeric variable, but it appears as text. I can see in DS there is a duration (seconds) numeric format, how can I convert a text variable into a numeric one?
It would be easier to convert the fields in a Google Sheet, but I need them as HH:MM for other calculations.
Try this:
0) Create a new Calculated Field
1) Seconds
Use a formula to convert the Time values to a single value in Seconds, where HH:MM:SS represents the field name:
( CAST(REGEXP_EXTRACT(HH:MM:SS, "^(\\d{2})") AS NUMBER ) * 60 * 60 ) + ( CAST(REGEXP_EXTRACT(HH:MM:SS, ":(\\d{2}):") AS NUMBER ) * 60 ) + CAST(REGEXP_EXTRACT(HH:MM:SS, "(\\d{2})$") AS NUMBER )
2) Change Field Type
- Numeric > Duration (sec.)
Credit to Google support community
You can use the TODATE or MINUTE and SECOND function into a calculated field to extract minutes and second from a date. However don't expect to display minutes and second datapoint on a line chart in Data Studio, timeserie charts only support hour-level data at a minimum.

Compute differece between timestamp values in secoonds

I want to calculate the frame per second value by computing the number of images captured in 1 second. For this purpose, I want to use the Timestamps given by the camera.
I want to subtract the current Timestamp value from the initial Timestamp value to calculate the number of seconds elapsed.
An example of my Timestamp values looks like 788343977.
QUESTIONS: How can I subtract the timestamps values to calculate the time elapsed in seconds?
Since you get the timestamp t1 = 788343977 by running 3 seconds and you get the timestamp t2 = 1999854657 when running 9 seconds, a second is equivalent to almost (approximation errors) s = (t2-t1)/6 = 201918446. So your might be dealing with nanoseconds.
Try this out :
def t_elapsed(t, t_init):
if t < t_init :
raise ValueError("Given timestamp smaller than initial one")
else:
return (t-t_init)/float(1e9)

BIRT report cross tabs: How to calculate and display durations of time?

I have a BIRT report that displays some statistics of calls to a certain line on certain days. Now I have to add a new measeure called "call handling time". The data is collected from a MySQL DB:
TIME_FORMAT(SEC_TO_TIME(some calculations on the duration of calls in seconds),'%i:%s') AS "CHT"
I fail to display the duration in my crosstab in a "mm:ss"-format even when not converting to String. I can display the seconds by not converting them to a time/string but that's not very human readable.
Also I am supposed to add a "grand total" which calculates the average over all days. No problem when using seconds but I have no idea how to do that in a time format.
Which data types/functoins/expressions/settings do I have to use in the query, Data Cube definition and the cross tab cell to make it work?
Time format is not a duration measure, it cannot be summarized or used for an average. A solution is to keep "seconds" as measure in the datacube to compute aggregations, and create a derived measure for display.
In your datacube, select this "seconds" measure and click "add" to create a derived measure. I would use BIRT math functions to build this expression:
BirtMath.round(measure["seconds"]/60)+":"+BirtMath.mod(measure["seconds"],60)
Here are some things to watch out for: seconds are displayed as single digit values (if <10). The "seconds" values this is based on is not an integer, so I needed another round() for the seconds as well, which resulted in seconds sometimes being "60".
So I had to introduce some more JavaScript conditions to display the correct formatting, including not displaying at all if "0:00".
For the "totals" column I used the summary total of the seconds value and did the exact same thing as below.
This is the actual script I ended up using:
if (measure["seconds"] > 0)
{
var seconds = BirtMath.round(BirtMath.mod(measure["seconds"],60));
var minutes = BirtMath.round(measure["seconds"]/60);
if(seconds == 60)
{
seconds = 0;
}
if (seconds < 10)
{
minutes + ":0" + seconds;
}
else
{
minutes + ":" + seconds;
}
}

Oracle DateAdd() of 2 different columns

I'm trying to figure out a DateAdd() equivalent in Oracle that is actually the difference in seconds between 2 columns in the same table:
SELECT
DISTINCT p.packet_id,
p.launch_dt,
r.route_duration,
s.completion_date,
DATEADD(SS, r.route_duration, p.launch_dt) AS tempDate
FROM
tdc_arc_apprpkt_def p
JOIN tdc_arc_inpr_route_def r
ON p.packet_id = r.packet_id
JOIN tdc_arc_inpr_route_step_detai s
ON p.packet_id = s.packet_id
AND s.completion_date > DATEADD(SS, r.route_duration, p.launch_dt)
Any help would be greatly appreciated!
In addition to being able to do date arithmetic using fractions of days as Tony demonstrates, assuming you are using 9i or later, you can also use interval functions (or, even better, define the ROUTE_DURATION column as an interval) and add intervals to dates. In your case, you can do
p.launch_dt + numtodsinterval( r.route_duration, 'SECOND' )
to add route_duration seconds to launch_dt.
If you were to define the route_duration column as an INTERVAL DAY TO SECOND rather than a NUMBER, you could simply add it to a date
p.launch_dt + r.route_duration
If I understand you correctly, you want to add r.route_duration seconds to p.launch_dt? In that case the expression is:
p.launch_dt + (r.route_duration/24/60/60)
Oracle DATE arithmetic works in days, so the divisions by 24, 60 and 60 convert the route_duration value from seconds to days.

Resources