i have data like this
no_shift start_time end_time
1 08:00:01 15:00:00
2 15:00:01 20:00:00
3 20:00:01 03:00:00
4 03:00:01 08:00:00
i am using this syntax:
select * from shift_time where `20:15:22` between start_time and end_time
i got null.. but if i changed the value to
08:01:22 return 1
16:35:12 return 2
05:11:23 return 4
but if 22:02:22 i got null. how to solve this problem?
Related
I aim to first achieve this
id
employee
Datelog
TimeIn
TimeOut
Hours
Count
5
Two
2022-08-10
09:00:00
16:00:00
07:00:00
1
4
Two
2022-08-09
09:00:00
16:00:00
07:00:00
1
3
Two
2022-08-08
09:00:00
16:00:00
07:00:00
1
2
One
2022-08-05
09:00:00
16:00:00
07:00:00
1
1
Two
2022-08-04
09:00:00
10:00:00
01:00:00
0
and now my main objective here is to give a bonus of 2k to employees whose Totalcount per month >=3.
employee
Month
TotalCount
Bonus
Two
August
3
2000
One
August
1
0
Here's the answer using Postgres. It's pretty much generic other than extracting the month out of datelog that might have a slightly different syntax.
select employee
,max(date_part('month', datelog ))
,count(*)
,case when count(*) >= 3 then 2000 else 0 end as bonus
from t
where hours >= time '06:00:00'
group by employee
employee
max
count
bonus
Two
8
3
2000
One
8
1
0
Fiddle
I have DT, which is a datetime64 Series:
0 2019-12-12 18:43:00
1 2019-03-22 18:30:00
2 NaT
3 2019-04-17 02:00:00
4 2009-03-15 18:00:00
5 2019-04-02 20:25:00
6 2019-05-01 11:00:00
7 2019-04-10 17:00:00
8 1973-07-14 22:00:00
9 2019-06-06 19:00:00
10 2019-06-18 21:00:00
11 2019-06-12 22:00:00
12 2019-06-11 22:00:00
13 2018-06-15 01:00:00
14 1999-08-15 02:30:00
...
88110 2019-10-01 22:00:00
88111 2019-10-01 22:45:00
88112 2019-10-02 01:00:00
88113 2019-10-02 03:26:00
88114 2019-10-02 03:26:00
88115 2019-10-02 05:33:00
88116 2019-10-02 06:35:00
88117 2019-10-02 12:00:00
88118 2019-10-02 19:00:00
88119 2019-10-02 19:15:00
88120 2019-10-02 20:00:00
88121 2019-10-02 20:00:00
88122 2019-10-02 20:03:00
88123 2019-10-02 22:00:00
88124 2019-10-02 22:00:00
Name: date_time, Length: 88125, dtype: datetime64[ns]
and a piece of code:
DT[DT.between("2019-12-05", "2019-12-08") & DT.dt.weekday == 1].dt.weekday.value_counts()
which yields:
5 27
3 23
4 19
Name: date_time, dtype: int64
which includes 3, 4 and 5 days but not a single requested day 1!
So, when I code just:
DT[DT.between("2019-12-05", "2019-12-08")].dt.weekday
it yields:
3821 3
87138 3
87139 3
87140 3
87141 3
..
87328 5
87329 5
87330 5
87331 5
87332 5
which is logical because we have 3 days interval, which corresponds to 3 week days. And yes, we do not have week day 1 at all in our days range! So why does this & DT.dt.weekday == 1 filter not work?
Thank you a lot for your time!
UPDATE
When I try to use any other filter like & DT.dt.weekday == 2, & DT.dt.weekday == 3 etc., I get an empty Series as a result of the filtering like this:
DT[DT.between("2019-12-05", "2019-12-08") & DT.dt.weekday == 4]
Moreover, DT.dt.weekday == 1 returns normal True/False list!
Maybe, we cannot filter by dt.(...) parameters?
Turns out that this:
DT[DT.between("2019-12-05", "2019-12-08") & DT.dt.weekday == 1]
is performed as this:
DT[ (DT.between("2019-12-05", "2019-12-08") & DT.dt.weekday) == 1 ]
which is why DT.dt.weekday the filter returned True for each day between 2019-12-05 and -08 because & DT.dt.weekday never really influenced as it was 3 to 5 for all the mentioned days range.
So, when I coded it like this:
DT[ (DT.between("2019-12-05", "2019-12-08")) & (DT.dt.weekday == 1) ]
everything worked out as was expected, i.e. nothing was chosen. But this, on the other hand:
DT[ (DT.between("2019-12-05", "2019-12-08")) & (DT.dt.weekday == 3) ]
yielded resulted in a few lines corresponding to day 3.
So, once parentheses are correctly put to separate A and B statements in A & B filtering expression, everything works as designed!
Thank you all for your time anyway! =)
I have a table as:
Id start_timestamp end_timestamp
1 2021-07-12 03:00:00 2021-07-13 11:58:05
2 2021-07-13 04:00:00 2021-07-13 05:00:00
3 2021-07-13 04:00:00 2021-07-13 09:00:00
4 2021-07-13 04:00:00 NULL
5 2020-04-10 04:00:00 2020-04-10 04:01:00
....
I want to find all records that fall between two specific timestamps? Basically I'm looking to understand what process ran during a high pick time of the day (it doesn't matter if they have 1 sec in the window or hours.. just occurrence in the window is enough)
So if the timestamps are 2021-07-13 00:00:00 to 2021-07-13 04:30:00
The query will return
1
2
3
4
How can I do that with SQL? (Preferably Presto)
This is the overlapping range problem. You may use:
SELECT *
FROM yourTable
WHERE
(end_timestamp > '2021-07-13 00:00:00' OR end_timestamp IS NULL) AND
(start_timestamp < '2021-07-13 04:30:00' OR start_timestamp IS NULL);
My answer assumes that a missing start/end timestamp value in the table logically means that this value should not be considered. This seems to be the logic you want here.
I have table T1
ID SCHEDULESTART SCHEDULEFINISH
1 2018-05-12 14:00:00 2018-05-14 11:00:00
2 2018-05-30 14:00:00 2018-06-01 11:00:00
3 2018-02-28 14:00:00 2018-03-02 11:00:00
4 2018-02-28 14:00:00 2018-03-01 11:00:00
5 2018-05-30 14:00:00 2018-05-31 11:00:00
I want to select all rows where difference in days (it's not important difference in hours) is greater than 1 day.
If SCHEDULESTART or SCHEDULEFINISH are on the same day or SCHEDULEFINISH is on next day then these rows should NOT be selected.
So the result should return rows with IDs: 1 2 3
because first row have difference in two days, second row (1st June is 2 days after 30th May ) and 3rd row (2nd March is 2 days after 28 February).
Is this possible somehow?
I know the function DAY but this will return only day number in that one month!!!
I must beging my query with
SELECT ID FROM T1 WHERE ...
Thanks in advance
In DB2, this should work:
select t1.*
from t1
where date(schedulestart) < date(schedulefinish) - 1 day;
I have a table with date(date), left time(varchar2(4)) and arrival time(varchar2(4)). Time taken is in 24 hour format as hhmm. If a person travel 3 times a day, what will be the query to calculate total travel time in a day?
I am using oracle 11g. Kindly help. Thank you.
Convert the value to a number and report in minutes:
select to_number(substring(time, 1, 2))*60 + to_number(substring(time, 3, 2)) as minutes
Your query would look something like:
select person, sum(to_number(substring(time, 1, 2))*60 + to_number(substring(time, 3, 2))) as minutes
from t
group by person;
I see no reason to convert this back to a string -- or to even store the value as a string instead of as a number. But if you need to, you can reverse the process to get a string.
There are 2 answers, If you want to sum time only on date then it can be done as:-
select curr_date,
sum(24 * (to_date(arrival_time, 'HH24:mi:ss')- to_date(left_time, 'HH24:mi:ss'))) as difference
from sql_prac group by curr_date,arrival_time,left_time;
The sample output is as follows:-
select curr_date,left_time,arrival_time from sql_prac;
CURR_DATE LEFT_TIME ARRIVAL_TIME
--------- -------------------- --------------------
30-JUN-17 00:00:00 15:00:00
30-JUL-17 03:30:00 11:30:00
30-AUG-17 03:00:00 12:30:00
30-SEP-17 04:00:00 17:00:00
30-JUN-17 00:00:00 15:00:00
30-JUL-17 03:30:00 11:30:00
30-AUG-17 03:00:00 12:30:00
30-SEP-17 04:00:00 17:00:00
30-SEP-17 04:00:00 17:00:00
9 rows selected
select curr_date,sum(24 * (to_date(arrival_time, 'HH24:mi:ss')- to_date(left_time, 'HH24:mi:ss'))) as difference
from sql_prac group by curr_date,arrival_time,left_time;
CURR_DATE DIFFERENCE
--------- ----------
30-JUN-17 30
30-JUL-17 16
30-SEP-17 39
30-AUG-17 19
If you want to sum it by person and date then it can be done as:-
select dept,curr_date,sum(24 * (to_date(arrival_time, 'HH24:mi:ss')- to_date(left_time, 'HH24:mi:ss'))) as difference
from sql_prac group by dept,curr_date,arrival_time,left_time order by Dept;
The sample output is as follows:-
Data in table is:-
select dept,curr_date,left_time,arrival_time from sql_prac;
DEPT CURR_DATE LEFT_TIME ARRIVAL_TIME
-------------------- --------- -------------------- --------------------
A 30-SEP-17 04:00:00 17:00:00
B 30-SEP-17 04:00:00 17:00:00
C 30-AUG-17 03:00:00 12:30:00
D 30-DEC-17 04:00:00 17:00:00
A 30-SEP-17 04:00:00 17:00:00
B 30-JUL-17 03:30:00 11:30:00
C 30-AUG-17 03:00:00 12:30:00
D 30-SEP-17 04:00:00 17:00:00
R 30-SEP-17 04:00:00 17:00:00
Data fetched using the query
select dept,curr_date,sum(24 * (to_date(arrival_time, 'HH24:mi:ss')- to_date(left_time, 'HH24:mi:ss'))) as difference
from sql_prac group by dept,curr_date,arrival_time,left_time order by Dept;
DEPT CURR_DATE DIFFERENCE
-------------------- --------- ----------
A 30-SEP-17 26
B 30-JUL-17 8
B 30-SEP-17 13
C 30-AUG-17 19
D 30-SEP-17 13
D 30-DEC-17 13
R 30-SEP-17 13