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?
Related
My calendar year runs from 07-01-(of one year) to 06-30-(of the next year).
My SQLITE DB has a Timestamp column and it's data type is datetime and stores the timestamp as 2023-09-01 00:00:00.
What I'm trying to do is get the MAX date of the latest snowfall. For example, with my seasonal years beginning July-01 (earliest) and ending June 30 (latest), I want to find only the latest (MAX) date snowfall was recorded, regardless of the year, based on the month.
Say if out of five years (2017 to 2022) worth of data in the database and it snowed Mar 15, 2020. And there was no date greater than than this one in any year, then this would be the latest date regardless which year it fell.
I've been trying many variations of the below query. This query says it runs with no mistakes and returns "null" values. I'm using SQLITE DB Browser to write and test the query.
SELECT Timestamp, MAX(strftime('%m-%d-%Y', Timestamp)) AS lastDate,
snowDepth AS lastDepth FROM DiaryData
WHERE lastDepth <> 0 BETWEEN strftime('%Y-%m-%d', Timestamp,'start of year', '+7 months')
AND strftime('%Y-%m-%d', Timestamp, 'start of year', '+1 year', '+7 months', '- 1 day')
ORDER BY lastDate LIMIT 1
and this is what's in my test database:
Timestamp snowFalling snowLaying snowDepth
2021-11-10 00:00:00 0 0 7.2
2022-09-15 00:00:00 0 0 9.5
2022-12-01 00:00:00 1 0 2.15
2022-10-13 00:00:00 1 0 0.0
2022-05-19 00:00:00 0 0 8.82
2023-01-11 00:00:00 0 0 3.77
If it's running properly I should expect:
Timestamp
lastDate
lastDepth
2022-05-19 00:00:00
05-19-2022
8.82
What am I missing or is this not possible in SQLITE? Any help would be appreciative.
Use aggregation by fiscal year utilizing SQLite's feature of bare columns:
SELECT Timestamp,
strftime('%m-%d-%Y', MAX(Timestamp)) AS lastDate,
snowDepth AS lastDepth
FROM DiaryData
WHERE snowDepth <> 0
GROUP BY strftime('%Y', Timestamp, '+6 months');
See the demo.
I'd get season for each record first, snowfall date relative to record's season start date after this, and largest snowfall date relative to record's season start date finally:
with
data as (
select
*
, case
when cast(strftime('%m', "Timestamp") as int) <= 7
then strftime('%Y-%m-%d', "Timestamp", 'start of year', '-1 year', '+6 months')
else strftime('%Y-%m-%d', "Timestamp", 'start of year', '+6 months')
end as "Season start date"
from DiaryData
where 1==1
and "snowDepth" <> 0.0
)
, data2 as (
select
*
, julianday("Timestamp") - julianday("Season start date")
as "Showfall date relative to season start date"
from data
)
, data3 as (
select
"Timestamp"
, "snowFalling"
, "snowLaying"
, "snowDepth"
from data2
group by null
having max("Showfall date relative to season start date")
)
select
*
from data3
demo
You can use the ROW_NUMBER window function to address this problem, yet need to apply a subtle tweak. In order to account for fiscal years, you can partition on the year for timestamps slided 6 months further. In this way, ranges like [2021-01-01, 2021-12-31] will instead be slided to [2021-06-01, 2022-05-31].
WITH cte AS (
SELECT *, ROW_NUMBER() OVER(
PARTITION BY STRFTIME('%Y', DATE(Timestamp_, '+6 months'))
ORDER BY Timestamp_ DESC ) AS rn
FROM tab
)
SELECT Timestamp_,
STRFTIME('%d-%m-%Y', Timestamp_) AS lastDate,
snowDepth AS lastDepth
FROM cte
WHERE rn = 1
Check the demo here.
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
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
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.
I want to report sales, by month, for the past 24 months. Next month the 25th month set of numbers should drop off the report and reflect the last 24 months again. I'm not sure how to write the query to handle the year change. Here is what I have.
select [Amount] * -1 as 'Parts Not Sold On Service Order Current', 0 as 'Parts Sold On Service Order', [Document Date]
from [G_L Entry]
where [G_L Account No_] between '40000' and '49999'
and [Dimension code] = 'par'
and [Document No_] not like 'PSV%'
AND YEAR([Document Date]) = YEAR(CURRENT_TIMESTAMP)
AND MONTH([Document Date]) = MONTH(CURRENT_TIMESTAMP)
union
select [Amount] * -1 as 'Parts Not Sold On Service Order Current -1', 0 as 'Parts Sold On Service Order', [Document Date]
from [G_L Entry]
where [G_L Account No_] between '40000' and '49999'
and [Dimension code] = 'par'
and [Document No_] not like 'PSV%'
AND YEAR([Document Date]) = YEAR(CURRENT_TIMESTAMP)
AND MONTH([Document Date]) = MONTH(CURRENT_TIMESTAMP) -1
union
select [Amount] * -1 as 'Parts Not Sold On Service Order Current -2', 0 as 'Parts Sold On Service Order', [Document Date]
from [G_L Entry]
where [G_L Account No_] between '40000' and '49999'
and [Dimension code] = 'par'
and [Document No_] not like 'PSV%'
AND YEAR([Document Date]) = YEAR(CURRENT_TIMESTAMP)
AND MONTH([Document Date]) = MONTH(CURRENT_TIMESTAMP) -2
This starts repeating the same year results after going into the previous year. Month may only be 5 months since the previous year. Next month will be 6 months. How do I write the query to handle this?
Assuming you have your query working the way you want it to in every other aspect already, try adding the following to your WHERE statements:
AND DateDiff(Month, [Document Date], CURRENT_TIMESTAMP) < 24
You may have to play with it a bit to get it just right for your particular report (e.g., "< 23", "< 25", etc)
I'm not a mind-reader, but integrating this into your current WHERE statement may give you what you want and shorten your query to the following (without your unions):
select [Amount] * -1 as 'Parts Not Sold On Service Order Current', 0 as 'Parts Sold On Service Order', [Document Date]
from [G_L Entry]
where [G_L Account No_] between '40000' and '49999'
and [Dimension code] = 'par'
and [Document No_] not like 'PSV%'
AND DateDiff(Month, [Document Date], CURRENT_TIMESTAMP) < 24