teradata gaps between previous date and preceding date by date by employee - teradata

I want to get the gap time between records and make that a row.
first record Joe StartDate 5/23/21 8:46 AM EndDate 5/23/21 9 AM,
next record Joe start date 5/23/21 9:23 AM End Date 5/23/21 10:43 AM,
next record Joe start date 5/23/21 8:23 AM End Date 5/23/21 2:01 PM,
next record Joe start date 5/23/21 11:16 AM End Date 5/23/21 2:54 PM,
next record Joe start date 5/23/21 11:26 AM End Date 5/23/21 3:14 PM,
next record Joe start date 5/12/2022 10:17:47 AM End Date 5/12/20224:45:54 PM,
next record Suzy start date 5/2/2022 8:08:26 AM End Date 5/2/2022 2:01:07 PM,
next record Suzy start date 5/1/2022 2:33:09 PM End Date 5/1/2022 2:49:53 PM,
next record Suzy start date 5/1/2022 2:35:02 PM End Date 5/11/2022 3:14:33 PM,
next record Suzy start date 5/12/2022 10:39:23 AM End Date 5/12/2022 4:49:33 PM,
next record JuJu start date 5/2/2022 11:03:14 AM End Date 5/2/2022 1:06:34 PM,
next record JuJu start date 5/2/2022 11:17:26 AM End Date 5/2/2022 1:31:33 PM,
next record JuJu start date 5/2/2022 11:41:12 AM End Date 5/2/2022 1:48:16 PM
I've tried:
'code' Lag(STARTDATE) Over (PARTITION BY ASSIGNEDTO, STARTDATE ORDER BY ASSIGNEDTO,STARTDATE,ENDDATE) AS prev_date,
Max(EndDate) Over (PARTITION BY ASSIGNEDTO, EndDate ORDER BY ASSIGNEDTO, EndDate DESC) AS max_start,'code'
Sometimes there's no end date, so that's why I am trying to get the time between the start and end date and the start date previous record and start date of current record, but i also need to see if folks are down and not doing anything? So I have two calculations 1 for HTM = total time from start to End and another that should show time between task or records for that date by employee.
'code'
EXAMPLE RECORD:
ASSIGNEDTO AUDITDATETIME STARTDATE ENDDATE TASKNAME
Joe 12/10/2021 12:00:00 AM 12/10/2021 4:42:05 PM 12/10/2021 6:10:48 PM Case Review
jUjU 5/17/2022 12:00:00 AM 5/17/2022 10:50:07 AM null Initial Review
jUjU 5/17/2022 12:00:00 AM 5/17/2022 12:03:06 AM null Initial Review
Here I have to calculate between the 5/17 10:50 and 12:03 indicates
she completed it in hh.mm
BeBe 11/12/2021 12:00:00 AM 11/2/2021 9:23:31 AM 11/2/2021 12:38:25 PM Manual
BeBE 11/2/2021 12:00:00 AM 11/2/2021 4:04:16 PM 11/2/2021 4:15:09 PM Case Review
Here I have to show the gap time between 12:38PM and 4:04 PM

Related

Pull a timelines by product by instance into 1 row instead of many

I'm trying to pull the timeline for each function into 1 line for each car.
The car started in the factory,
Function - Start Date - End Date - Person
the frame was built on 05/23/2022 04:16 AM 05/23/2022 06:16 AM By Joe,
the Windshield was installed on 05/23/2022 9:18 PM 05/23/2022 10:18 PM By Suzy,
the Motor was installed on 05/25/2022 01:14 PM 05/25/2022 03:23 PM By Harry,
the Frame was Painted on 05/28/2022 9:45 PM 05/29/2022 9:45 PM by Joe.
for each car I want to take the individual records above and join into 1 row to show the timelines for this 1 card, which eventually will lead to help me find my bottle necks and processing time.
I'm trying to build two different sets of data. 1 how mayne windshields did joe do and whats joes downtime in between each windsheild, how long did it take us to build the car, which i can do some datediffs, but trying to get each car on 1 line form the different entry's.
i'm thinking i have to do some Lead / Lag.
Thinking my data would be 1 line:
Model T1 - frame - 05/23/2022 04:16 AM 05/23/2022 06:16 AM - Joe - Windsheild - 05/23/2022 9:18 PM - 05/23/2022 10:18 PM - Suzy - Motor Installed - 5/25/2022 01:14 PM - 05/25/2022 03:23 PM - Harry - Frame Painted 05/28/2022 9:45 - PM 05/29/2022 9:45 - Joe Total Time to Assemble 5:16 HH: MM
Select Insert DateTime as Production Start DateTime, StartDate as StartDate, Min(StartDate) Over (PARTITION BY ASSIGNEDTO, StartDate ORDER BY ASSIGNEDTO, StartDate DESC) AS FrameBuilt, Lag(StartDate) Over (PARTITION BY ASSIGNEDTO ORDER BY ASSIGNEDTO, StartDate DESC) AS WindsheildInstalled, Lead(EndDate) Over (PARTITION BY ASSIGNEDTO ORDER BY ASSIGNEDTO, EndDate DESC) AS WindsheildInstalledStarted, EndDate as WindsheildInstallCompleted, Lag(EndDate) Over (PARTITION BY ASSIGNEDTO ORDER BY ASSIGNEDTO, EndDate DESC) AS MotorInstalled, CASE WHEN Cast(ENDDATE AS DATE) + 1 = Cast(Lag_next_start AS DATE) THEN 0 ELSE 1 END AS flag, Case when Cast(EndDate as Date) = Cast(Startdate as Date) then 1 Else 0 End as count2, Case When EndDate = StartDate then 1 Else 0 End as count1,TaskName, (max_start - min_start) Hour(4) TO SECOND as htm, (max_start - min_start) Hour(4) TO SECOND as tbt,(tbt = Time between Task) From WorkflowLog as wfl left join functions_FILTERED as pdf on wfl.INTERNALID = pdf.INTERNALID QUALIFY (ROW_NUMBER() OVER(PARTITION BY wfl.INTERNALID ORDER BY STARTDATE DESC))=1) as a

Teradata Dates with a day and week

I have a User, I have records with a Date Timestamp it includes Day hour second, I want to for each User to count the number of hours they were in the system per that day, calculating the first record of the day and the last record of the day, then I want to do this for the week, so Column 1 = # of hours I was in the system for the day, column 2 = number of hours I am in the system for that week.
I know there is a simple way to do this and I am over looking the simplest thing i think.
Assigned To: = End User
Start Date = Date and Time for the record,
some records have a start date and time,
some have a start date and an end date.
If first record is 8/2/2021 8:43:49 AM then this is the first record,
if the last record for that day is 8/2/2021 3:52:58 PM
Calculate the hours and minutes between first record and last record.
Task StartDate EndDate ASSIGNEDTO
Effectuation 8/2/2021 7:16 frank.author
Case Review 8/2/2021 7:36 8/2/2021 7:38 james.stevo
Manual Outreach 8/2/2021 7:38 8/2/2021 10:46 james.stevo
Effectuation 8/2/2021 7:54 frank.author
Case Review 8/2/2021 8:00 8/2/2021 8:22 james.stevo
Manual Outreach 8/2/2021 8:23 8/2/2021 10:46 james.stevo
Manual Outreach 8/2/2021 8:33 8/2/2021 10:47 james.stevo
Manual Outreach 8/2/2021 8:38 8/2/2021 10:47 james.stevo
Effectuation 8/2/2021 8:51 frank.author
Case Review 8/2/2021 9:04 james.stevo
Manual Outreach 8/2/2021 9:18 8/2/2021 13:10 james.stevo
Case Review 8/2/2021 9:30 james.stevo
Case Review 8/2/2021 9:53 james.stevo
Manual Outreach 8/2/2021 10:43 8/2/2021 20:50 james.stevo
Manual Outreach 8/2/2021 11:03 8/2/2021 20:50 james.stevo
Case Review 8/2/2021 11:06 james.stevo
Case Review 8/2/2021 11:07 james.stevo
Case Review 8/2/2021 11:14 james.stevo
Case Review 8/2/2021 11:38 james.stevo
Effectuation 8/2/2021 11:38 frank.author
Manual Outreach 8/2/2021 12:03 8/2/2021 20:51 james.stevo
Case Review 8/2/2021 12:07 james.stevo
Manual Outreach 8/2/2021 12:23 8/2/2021 20:51 james.stevo
I put the 3 tables together
and was trying to get the min and max for the user,
sometimes there are 3 records for a user,
min date would be first login or min date and last maxdate would be last logout
Select AssignedTo as UserID,
Cast((STARTDATE) as Date) as UpdateDatetime,
min(STARTDATE) as minDate,
max(ENDDATE) as MaxDate ,
(MaxDate) - (minDate) hour to minute as htm,
'WorkflowLog' as Source
From WorkflowLog
Where INTERNALID Is Not Null
and ENDDATE Is Not NULL
group by 1,2
Union All
---- Task Completed
Select CREATEDBY as UserID,
Cast((AUDITDATETIME) as Date) as UpdateDatetime,
min(AUDITDATETIME) as minDate,
max(AuditDateTime) as MaxDate ,
(MaxDate) - (minDate) hour to minute as htm,
'Audit' as Source
From Audit
Where INTERNALID Is Not Null
and AuditDateTime Is Not NULL
group by 1,2
Union All
--- Task and Queue completed
Select CurrentAssignedTo as UserID,
Cast((UpdateDatetime) as Date) as UpdateDatetime,
min(UpdateDatetime) as minDate,
max(UpdateDatetime) as MaxDate ,
(MaxDate) - (minDate) hour to minute as htm,
'CaseMetadata' as Source
From CaseMetadata
Where INTERNALID Is Not Null
and UpdateDatetime Is Not NULL
group by 1,2 Select UserID,
Cast(UpdateDatetime as date)as UpdateDatetime,
min(minDate) as minDate,
max(MaxDate) as MaxDate ,
(MaxDate) - (minDate) hour to minute as htm
From users_In_Out
group by 1,5,2
Based on this post and a similar on Database Administrators
select
AssignedTo
,cast(StartDate as date) -- for each day
,min(StartDate) as minDate
,max(EndDate) as MaxDate
,MaxDate - minDate hour to minute -- difference max-min
from myTable
group by 1,2
Edit based on your answer:
WITH users_In_Out AS
(
Select AssignedTo as UserID,
Cast((STARTDATE) as Date) as UpdateDatetime,
min(STARTDATE) as minDate,
max(ENDDATE) as MaxDate ,
(MaxDate) - (minDate) hour to minute as htm,
'WorkflowLog' as Source
From WorkflowLog
Where INTERNALID Is Not Null
and ENDDATE Is Not NULL
group by 1,2
Union All
---- Task Completed
Select CREATEDBY as UserID,
Cast((AUDITDATETIME) as Date) as UpdateDatetime,
min(AUDITDATETIME) as minDate,
max(AuditDateTime) as MaxDate ,
(MaxDate) - (minDate) hour to minute as htm,
'Audit' as Source
From Audit
Where INTERNALID Is Not Null
and AuditDateTime Is Not NULL
group by 1,2
Union All
--- Task and Queue completed
Select CurrentAssignedTo as UserID,
Cast((UpdateDatetime) as Date) as UpdateDatetime,
min(UpdateDatetime) as minDate,
max(UpdateDatetime) as MaxDate ,
(MaxDate) - (minDate) hour to minute as htm,
'CaseMetadata' as Source
From CaseMetadata
Where INTERNALID Is Not Null
and UpdateDatetime Is Not NULL
group by 1,2
)
Select UserID,
UpdateDatetime,
min(minDate) as minDate,
max(MaxDate) as MaxDate ,
max(MaxDate) - min(minDate) hour to minute as htm
From users_In_Out
group by 1,2
Of course, calculating htm and source in the UNIONs is not needed for the final Select and could be removed.

How to query correctly a date period in firestore and cloudfunctions

I have a collection with the next item and one of their properties is the date:
id: xxxxxx
name: xxxx
date: August 21, 2018 at 1:00:00 AM UTC+8 (timestamp)
Inside a firebase cloud function I am trying to query all objects from a period, this period can be the day, the week, the year, etc.
I want to query the items by the current server day, so I do this in a Firebase Cloud Function:
let auxDate = moment();
dateStart = auxDate.startOf('day').toDate();
dateEnd = auxDate.endOf('day').toDate();
await admin.firestore().collection('items')
.where("date", ">=", dateStart)
.where('date', '<=', dateEnd).get();
The values of dateStart and dateEnd printed in the console are:
Date start: Tue Aug 21 2018 00:00:00 GMT+0000 (UTC)
Date end: Tue Aug 21 2018 23:59:59 GMT+0000 (UTC)
And the query return 0 items. But when I change the date of the item to
id: xxxxxx
name: xxxx
date: August 21, 2018 at 8:00:00 AM UTC+8 (timestamp)
The query return the item correctly.
So now i know the problem is about the Offset, but how can i fix this? Why firebase save all dates in UTC+8?
I found the answer to my own question. I will explain in detail.
Understand the problem:
First thing is to remember we are doing this query inside a Cloud Function so we do not know the client timezone. In Firestore all dates are saved in UTC+00. So if i have an item like:
id: xxxxxx
name: xxxx
date: August 22, 2018 at 12:00:00 AM UTC+8 (timestamp)
it means this date is equals to August 21, 2018 at 04:00:00 PM UTC+0.
So if i want to query all dates between:
Date start: August 22, 2018 at 12:00:00 AM UTC+8 (timestamp)
Date end: August 22, 2018 at 11:59:59 PM UTC+8 (timestamp)
It means the query MUST be like:
Date start: August 21, 2018 at 04:00:00 AM UTC+0 (timestamp)
Date end: August 22, 2018 at 03:59:59 PM UTC+0 (timestamp)
So the problem is when I do let auxDate = moment() this create a date with server time with no offset so the value of auxDate is for example: August 21, 2018 at 11:23:43 PM UTC+0.
So the starting dates from auxDate become:
auxDate start: August 21, 2018 at 12:00:00 AM UTC+0 (timestamp)
auxDate end: August 21, 2018 at 11:59:59 PM UTC+0 (timestamp)
As we can see this query do not fill the ranges of our MUST be query and many items will be out.
Problem conclusion:
We cannot query correctly if we do not know the time zone of the client.
Solution:
Now i pass to this cloud function the current time of the client and calculate the start and end dates:
const clientCurrentDateTime = "2018-08-22T11:23:15+08:00";
let startDate = moment(clientCurrentDateTime).utcOffset(clientCurrentDateTime).startOf('day').toDate();
let endDate = moment(clientCurrentDateTime).utcOffset(clientCurrentDateTime).endOf('day').toDate();
This creates the correct start and end date-times for different offsets/timezones

Moment.js check to see if a date-time is between a set of date and times

Thanks in advance...I'm new to using Moment and in the code below not sure why the check to isBetween is true. I see that the date portion is between the given dates but the time isn't. Not sure if I'm using the command correctly.
var beginningTime = moment('2016-11-25 3:45 am', 'YYYY-MM-DD h:mma');
var endTime1 = moment('2016-11-22 9:00 am', 'YYYY-MM-DD h:mma');
var endTime2 = moment('2016-11-30 1:00 pm', 'YYYY-MM-DD h:mma');
console.log("Beg Time = "+beginningTime.toString());
console.log("End Time1 = " +endTime1.toString());
console.log("End Time2 = "+endTime2.toString());
console.log(beginningTime.isBetween(endTime1,endTime2));
VM5431:4 Beg Time = Fri Nov 25 2016 03:45:00 GMT-0500
VM5431:5 End Time1 = Tue Nov 22 2016 09:00:00 GMT-0500
VM5431:6 End Time2 = Wed Nov 30 2016 13:00:00 GMT-0500
VM5431:7 true
As the docs says, isBetween:
Check if a moment is between two other moments, optionally looking at unit scale (minutes, hours, days, etc). The match is exclusive
The comparison takes in accout the full object (both date and time info) so the result for your example is correct (2016-11-25 3:45 am is between 2016-11-22 9:00 am and 2016-11-30 1:00 pm).
If you what to compare only time, your moment objects should have the same date values (day, month, year). One way to achieve that, starting from your moment objects (beginningTime, endTime1, endTime2), is using moment({unit: value, ...}) constructor. Setting only hour and minute properties, it will create a moment object for the current day.
hour() and minute() get hours and minutes for the given moment.
Here a working example:
var beginningTime = moment('2016-11-25 3:45 am', 'YYYY-MM-DD h:mm a');
var endTime1 = moment('2016-11-22 9:00 am', 'YYYY-MM-DD h:mm a');
var endTime2 = moment('2016-11-30 1:00 pm', 'YYYY-MM-DD h:mm a');
console.log("Beg Time = "+beginningTime.toString());
console.log("End Time1 = " +endTime1.toString());
console.log("End Time2 = "+endTime2.toString());
console.log(beginningTime.isBetween(endTime1,endTime2));
// Create a moment object considering only hour and minute
function getMomentTime(m){
return moment({hour: m.hour(), minute: m.minute()});
};
var t1 = getMomentTime(endTime1);
var t2 = getMomentTime(endTime2);
var begin = getMomentTime(beginningTime);
console.log("Beg Time = "+begin.toString());
console.log("End Time1 = " +t1.toString());
console.log("End Time2 = "+t2.toString());
console.log(begin.isBetween(t1, t2));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.16.0/moment.min.js"></script>

VB.net Addmonths

I'm trying to move Dates, i.e. Go back a month or forward a month. If I move back a month as of this month (February) to January, it holds February's last day (28th) the same if I add a month. The start Date is fine as all month's start on the 1st.
Please could anyone advise.Thanks
Vb
hStartDate.Value = "2013/2/01 12:00:00 AM"
hEndDate.Value = "2013/2/28 11:59:59 PM"
Dim StartDate As New Date
StartDate = hStartDate.Value
hStartDate.Value = StartDate.AddMonths(-1)
Dim EndDate As New Date
EndDate = hEndDate.Value
hEndDate.Value = EndDate.AddMonths(-1)
Output
hStartDate.Value = "2013/01/01"
hEndDate.Value = "2013/01/28 11:59:59 PM"
Try this to set the end date (after hStartDate is modified):
hEndDate.Value = New Date(hStartDate.Year, hStartDate.Month, Date.DaysInMonth(hStartDate.Year, hStartDate.Month))
The function Date.DaysInMOnth() returns the last day of a month in a year given. You could set your date like this too if you prefer:
hEndDate.Value = EndDate.AddMonths(-1)
hEndDate.Value.Day = Date.DaysInMonth(hStartDate.Year, hStartDate.Month)
This should help: DevCurry it's basically taking the 1st day of the current month and removing one day thus resulting in the last day of the previous month.
hStartDate.Value = "2013/2/01 12:00:00 AM"
hEndDate.Value = "2013/2/28 11:59:59 PM"
Dim StartDate As New Date
StartDate = hStartDate.Value
hStartDate.Value = StartDate.AddMonths(-1)
Dim EndDate As New Date
EndDate = hEndDate.Value
hEndDate.Value = StartDate.AddDays(-1)

Resources