Teradata Dates with a day and week - teradata

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.

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 gaps between previous date and preceding date by date by employee

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

SQLITE date offset

I am trying to replicate the following, which is written in some other flavor of SQL:
where contact_date <= open_date
and contact_date >= dateadd(days, -30, open_date)
And below is my SQLITE query, I have replaced dateadd with SQLITE date analog, but not behaving as expected. I made sure the columns were 'DATE' type, but when I use the date function to make a 30 day offset, it just NULLs out the date.
Here is my query now:
SELECT Template, "Outcome Date" as contact_date,"Open Date" as open_date,
date("Open Date",'-30 day') window_date_start
--cast("Outcome Date" as integer) contact_date_int, cast("Open Date" as integer) open_date_int,
--strftime('%m/%d/%Y %H:%M', "Outcome Date") as contact_date_strft, strftime('%m/%d/%Y %H:%M', "Open Date") as open_date_strft
from Emails e
join Listing l
on l."Onovative Id" = e."Onovative Id"
where
contact_date <= open_date
--and contact_date > date(open_date,'-30 day'); <=== Not working
Output
Template contact_date open_date window_date_start <=== cannot really see below, but NULL
product offer loans auto runoff email 5/20/2020 11:02 6/19/2020 0:00
onboarding digital banking email 5/22/2020 11:02 6/24/2020 0:00
onboarding digital banking email 5/22/2020 11:02 5/5/2020 0:00
product offer loans auto runoff email 5/22/2020 11:02 6/25/2020 0:00
onboarding deposits checking 100 email 5/29/2020 11:06 9/24/2020 0:00
onboarding deposits checking 100 email 5/29/2020 11:06 5/5/2020 0:00
onboarding digital banking email 5/29/2020 11:06 9/9/2020 0:00
onboarding digital banking email 5/29/2020 11:06 6/9/2020 0:00
product offer loans auto runoff email 5/29/2020 15:12 9/30/2020 0:00
...
Anyone know how to offset dates in SQLITE?

How to query data based on a date using Fiscal Year on Sqlite

I'm fixing an android app for a client, and I need to query an sqlite database in order to show a report they need.
The report should only show data for the current fiscal year (begins April-1 ends 31-March). A previous developer did this query:
... WHERE sales_date BETWEEN date('now','localtime','start of year','+3 months') "
+ "AND date('now','localtime','start of year', '+1 year', '+3 months','-1 day') "
This query doesn't work right now since for example in January 2015 it shows data from April 2015 until March 2015, where it should show data from April 2014 until March 2015.
I'm looking for an optmized solution for sqlite since I've found similar answers for other SQL's (Truncate date to fiscal year or Use TO_CHAR function to display the Fiscal Year)
In the first three months, you want the previous year.
So subtract three months before you go the start of the year:
BETWEEN date('now', 'localtime', '-3 months', 'start of year', '+3 months') "
AND date('now', 'localtime', '-3 months', 'start of year', '+1 year', '+3 months', '-1 day')

Day and night averages SQlite

I have a db with many years of ambient temperature, I need to make the average temperature from every month from 06:00 to 20:00 (day) and night. What I already made is
SELECT
STRFTIME('%Y', fecha) AS year,
STRFTIME('%m', fecha) AS month,
AVG(T10_AVG) AS Temp_prom,
FROM GER
GROUP BY STRFTIME('%Y', fecha), STRFTIME('%m', fecha)
But I have no idea how to include the day and night averages.
If you want to get those averages in the same monthly output record, you cannot add another GROUP BY column.
You have to compute these values with separate subqueries:
SELECT strftime('%Y', fecha) AS year,
strftime('%m', fecha) AS month,
(SELECT AVG(T10_AVG)
FROM Ger AS g2
WHERE g2.fecha GLOB strftime('%Y-%m*', Ger.fecha)
AND (g2.hour >= '06' AND g2.hour < '20')
) AS avg_day,
(SELECT AVG(T10_AVG)
FROM Ger AS g2
WHERE g2.fecha GLOB strftime('%Y-%m*', Ger.fecha)
AND (g2.hour < '06' OR g2.hour >= '20')
) AS avg_night,
AVG(T10_AVG) AS Temp_prom
FROM Ger
GROUP BY year, month

Resources