Creating seasonal subset - r

Having a little trouble with creating seasonal subsets in r.
The datetime is already in the POSIXct format so I didn't think it necessary to add the as.POSIXct() function.
Also, the dataset is already organized by datetime.
This is what the current code looks like.
summer_subset <- subset(YTD_v5, YTD_v5$started_at >= '2021-06-21 00:00:00' & YTD_v5$ended_at <= '2021-09-21 23:59:59')
fall_subset <- subset(YTD_v5, YTD_v5$started_at >= '2021-09-22 00:00:00' & YTD_v5$ended_at <= '2021-12-20 23:59:59')
winter_subset <- subset(YTD_v5, (YTD_v5$started_at >= '2021-12-21 00:00:00' & YTD_v5$ended_at <= '2022-02-28 23:59:59') | (YTD_v5$started_at >= '2021-03-01 00:00:00' & YTD_v5$ended_at <= '2021-03-19 23:59:59'))
spring_subset <- subset(YTD_v5, YTD_v5$started_at >= '2021-03-20 00:00:00' & YTD_v5$ended_at <= '2021-06-20 23:59:59')
When I view the summer_subset, the rows start at 2021-06-21 04:00:00, not 00:00:00. The final entry is 2021-09-21 03:55:00, not 23:59:59.
In the YTD_v5 dataset, there are entries that contain start times at 00:00:00 and end times that end at 23:59:59.
Thanks for any insight in advanced.

Related

SQLITE results inconsistency

I am summarising the outputs of a survey, stored in a sqlite database file, and have a view defined as follows - this is meant to show entries in the valid response view where the respondent has indicated that EITHER:
(a) they are meeting the requirements already; OR,
(b) they aren't meeting all requirements, but the associated actions in place will be complete by the end of the year (31/12/2020):
CREATE VIEW complete_dec20 AS
SELECT *
FROM valid_response
WHERE
(impact_answer NOT IN ("Fully","Yes","N/A") AND
td__update_leg_doc <= "2020-12-31" AND
td__update_proc <= "2020-12-31" AND
td__update_op_proc <= "2020-12-31" AND
td__update_tech <= "2020-12-31" AND
td__training <= "2020-12-31") OR
impact_answer IN ("Fully","Yes","N/A")
The records included in the view are correct, however, when I query the results from the valid_response view that are not included in the view, there are some strange results:
SELECT *
FROM valid_response
WHERE id NOT IN (SELECT id FROM complete_dec20);
e.g.
id,impact_answer,td__update_leg_doc,td__update_proc,td__update_op_proc,td__update_tech,td__training
7,Partially,2020-12-31,,,,
Based on the date of 2020-12-31 and answer of 'Partially', this should be in the complete_dec20 view.
Can you explain why it isn't / what I'm missing?
Based on the date of 2020-12-31 and answer of 'Partially', this should
be in the complete_dec20 view
This should be in the complete_dec20 view only if all of these conditions are true:
td__update_leg_doc <= '2020-12-31' AND
td__update_proc <= '2020-12-31' AND
td__update_op_proc <= '2020-12-31' AND
td__update_tech <= '2020-12-31' AND
td__training <= '2020-12-31'
Are they?
I don't think so.
If they were true then the id would be returned by complete_dec20.
Also, the WHERE clause of complete_dec20 can be a bit simpler because there is no need to check impact_answer NOT IN ('Fully','Yes','N/A'):
CREATE VIEW complete_dec20 AS
SELECT *
FROM valid_response
WHERE impact_answer IN ('Fully','Yes','N/A')
OR
(
td__update_leg_doc <= '2020-12-31' AND
td__update_proc <= '2020-12-31' AND
td__update_op_proc <= '2020-12-31' AND
td__update_tech <= '2020-12-31' AND
td__training <= '2020-12-31'
)
Or even simpler with the function MAX():
CREATE VIEW complete_dec20 AS
SELECT *
FROM valid_response
WHERE impact_answer IN ('Fully','Yes','N/A')
OR
MAX(
td__update_leg_doc,
td__update_proc,
td__update_op_proc,
td__update_tech,
td__training
) <= '2020-12-31'

Show time diff as HH:MM:SS between two datetimes in SQLite

I got two different datetimes: 2020-05-18 12:30:01 and 2020-05-17 13:00:00.
I want to show the time difference between them in the format HH:MM:SS, which is 23:30:01.
If the difference is higher than 24 hours, let's say 28 hours, 12 minutes and 45 seconds, it would show like 28:12:45.
How can I do that in SQLite?
SQLite supports a limited number of functions for datetime manipulation.
One of these functions is strftime(), and
strftime('%s', somedate)
returns the number of seconds from '1970-01-01' up to somedate.
With the use of this function, arithmetic calculations, string padding and concatenations you can get what you want like this:
CASE WHEN ((strftime('%s', date1) - strftime('%s', date2)) / 3600) < 10 THEN '0' ELSE '' END ||
((strftime('%s', date1) - strftime('%s', date2)) / 3600) || ':' ||
SUBSTR('0' || (((strftime('%s', date1) - strftime('%s', date2)) / 60) % 60), -2) || ':' ||
SUBSTR('0' || ((strftime('%s', date1) - strftime('%s', date2)) % 60), -2)
Replace date1 and date2 with your dates.
See a simplified demo.
SELECT time(
(
julianday('2020-05-18 12:30:01')-
julianday('2020-05-17 13:00:00')
)*60*60*24, 'unixepoch'
);
answsers the question when time difference is lower than 24h...

select between date range within speicific time period

I have two seperate columns for date and time each being saved in varchar2
I'm trying to query a specific range of time:
i.e. 1/1/2017 - 1/31/2017
between 6PM-6AM each day
So far I did this:
select * from (select a.*,TO_DATE(billdate||' '||billtime,'YYYY/MM/DD HH24:Mi:SS')
as Timex from billtable a where billdate >= '2017/01/01' and billdate <= '2017/01/31')
where timex>=to_date(''2017/01/01 18:00:00','YYYY/MM/DD HH24:Mi:SS')
and timex<=to_date('2017/01/31 06:00:00','YYYY/MM/DD HH24:Mi:SS')
order by billdate
What can I do further or Is It the wrong way Iam going?
Thanks!
Assuming you're stuck with the data model you have (storing dates and/or times as strings, or separately, is not a good idea) and that you are not interested in the six hours before and after the date range, the formats you've used at least allow you to query those ranges fairly simply:
select a.*, to_date(billdate||' '||billtime,'YYYY/MM/DD HH24:Mi:SS') as timex
from billtable a
where billdate >= '2017/01/01'
and billdate <= '2017/01/31'
and (billtime <= '06:00:00' or billtime >= '18:00:00')
order by billdate, billtime;
With some sample data provided in a CTE:
alter session set nls_date_format = 'YYYY-MM-DD HH24:MI:SS';
with billtable (billdate, billtime) as (
select '2017/01/01', '00:00:00' from dual
union all select '2017/01/01', '06:00:00' from dual
union all select '2017/01/01', '06:00:01' from dual
union all select '2017/01/31', '17:59:59' from dual
union all select '2017/01/31', '18:00:00' from dual
union all select '2017/01/31', '23:59:59' from dual
)
select a.*, to_date(billdate||' '||billtime,'YYYY/MM/DD HH24:Mi:SS') as timex
from billtable a
where billdate >= '2017/01/01'
and billdate <= '2017/01/31'
and (billtime <= '06:00:00' or billtime >= '18:00:00')
order by billdate, billtime;
BILLDATE BILLTIME TIMEX
---------- -------- -------------------
2017/01/01 00:00:00 2017-01-01 00:00:00
2017/01/01 06:00:00 2017-01-01 06:00:00
2017/01/31 18:00:00 2017-01-31 18:00:00
2017/01/31 23:59:59 2017-01-31 23:59:59
If you already had a date, or were converting to a date - or in fact a timestamp to make this work - you could do:
select billdate, billtime, cast(timex as date)
from (
select a.*, to_timestamp(billdate||' '||billtime,'YYYY/MM/DD HH24:Mi:SS') as timex
from billtable a
where billdate >= '2017/01/01' and billdate <= '2017/01/31'
)
where extract(hour from timex) < 6
or (extract(hour from timex) = 6 and extract(minute from timex) = 0 and extract(second from timex) = 0)
or extract(hour from timex) >= 18
order by timex;

I want to create a reminder of records in asp of before 4 months from today's date in SQL Server

I have written this query :
SELECT
MemberId
,Title
,LastName
,FirstName
,MiddleName
,Occupation
,Productof
,Dateofpurchase
,Dateofservice
,Address
,City
,Pin
,Phone
,MobileNo1
,Email
FROM
tbmMember
WHERE
Dateofservice <= DATEADD(MONTH, - 4, GETDATE())
but this query returning all the records of before 4 months.
Try this, it return before 4 month data from today
WHERE Dateofservice >= DATEADD(MONTH,-4,GETDATE())
This is time (hours, minute,...) sensitive:
SELECT MemberId,Title,LastName,FirstName,MiddleName,Occupation,Productof,Dateofpurchase,Dateofservice,Address,City,Pin,Phone,MobileNo1,Email
FROM tbmMember
WHERE Dateofservice >= DATEADD(MONTH,-4,GETDATE())
AND Dateofservice <= GETDATE()
This one is not time sensitive:
SELECT MemberId,Title,LastName,FirstName,MiddleName,Occupation,Productof,Dateofpurchase,Dateofservice,Address,City,Pin,Phone,MobileNo1,Email
FROM tbmMember
WHERE Dateofservice >= CONVERT(date, DATEADD(MONTH,-4,GETDATE()))
AND Dateofservice <= CONVERT(date, GETDATE())
Assuming Dateofservice is date type.
------------- EDIT -----------------
If you just needs the data of 4 months back:
SELECT MemberId,Title,LastName,FirstName,MiddleName,Occupation,Productof,Dateofpurchase,Dateofservice,Address,City,Pin,Phone,MobileNo1,Email
FROM tbmMember
WHERE Dateofservice = CONVERT(date, DATEADD(MONTH,-4,GETDATE()))
Note that DateofService must be date type, otherwise you need to convert that as date by CONVERT(date, Dateofservice) as well
If you simply want to compare the date of your records with a date four months back from today's date then here is how you can achieve it:
SELECT
MemberId
,Title
,LastName
,FirstName
,MiddleName
,Occupation
,Productof
,Dateofpurchase
,Dateofservice
,Address
,City
,Pin
,Phone
,MobileNo1
,Email
FROM
tbmMember
WHERE
CONVERT(Date,Dateofservice) = CONVERT(Date, DATEADD(MONTH, - 4, GETDATE()))

Finding minimum in teradata if values can be null also

I want to find minimum of six values in teradata. Following is the logic I am using but it fails when there is a null value in either of the fields. I would appreciate your help on this. Thanks in advance.
CASE WHEN event.A1 <= event.A2 and event.A1 <= event.A3 and event.A1 <= event.A4 and event.A1
<= event.A5 and event.A1 <= event.A6 THEN event.A1
WHEN event.A2 <= event.A3 and event.A2 <= event.A4 and event.A2 <= event.A
WAITING_PICKUP_NOTELEFT and event.A2 <= event.A6 THEN event.A2
WHEN event.A3 <= event.A4 and event.A3 <= event.A5 and event.EXC_DELI
VERY_ATTEMPT <= event.A6 THEN event.A3
WHEN event.A4 <= event.A5 and event.A4 <= event.A6 THEN event.EXC_DELIVER
Y_ATT_NOTE
WHEN event.A5 <= event.A6 THEN event.A5
ELSE coalesce(event.A6,event.A5,event.A4,event.A3,event.A2,ev
ent.A1) END AS ZZZ
I think I'd use a derived table to handle all the null logic one time, and then select from that. Something like:
SELECT
CASE WHEN t1.a1 <= t1.a2 and t1.a1 <= t1.a3...
FROM
(Select
coalesce(event.A1,0), --don't know what the data type is, replace accordingly
coalesce(event.A2,0),
...) t1
It's still kind of ugly, but I think it will still be simpler.
EDIT:
I may be oversimplifying, but if you're on TD 14, you could look into the LEAST function:
SELECT LEAST(event.a1,event.a2,event.a3,...)

Resources