I have the following scenario:
I have employees who register their check in/out from their work. But they have 10 minutes of tolerance.
The late entries I get with this view:
CREATE OR REPLACE VIEW employees_late_entries
(
id,
created_datetime,
entry_datetime,
contact_id,
contact_name,
user_id,
employees_perm_id
)
AS
SELECT precence_records.id,
precence_records.created AS created_datetime,
("substring"(precence_records.created::text, 0, 11) || ' '::text) || contacts.entry_time::text AS entry_datetime,
contacts.id AS contact_id,
contacts.name AS contact_name,
precence_records.user_id,
precence_records.employees_perm_id
FROM precence_records,
contacts
WHERE
precence_records.type::text = 'entry'::text AND
contacts.employee = true AND
contacts.id = precence_records.contact_id AND
( ("substring"(precence_records.created::text, 0, 11) || ' '::text) || contacts.entry_time::text) < precence_records.created::text AND
precence_records.employees_perm_id IS NULL;
the precence_records.created is the check in time and contacts.entry_time its the time of the schedule entry time for the employee.
This is the condition contacts.entry_time vs precence_records.created to get the late entries:
( ("substring"(precence_records.created::text, 0, 11) || ' '::text) || contacts.entry_time::text) < precence_records.created::text
So I wanna do something like that:
( ("substring"(precence_records.created::text, 0, 11) || ' '::text) || (contacts.entry_time::text + 10 MINUTES) ) < precence_records.created::text
DATA TYPES:
precence_records.created TIMESTAMP
contacts.entry_time VARCHAR
Can you help me please
Dates, Times and Timestamps in PostgreSQL can be added/subtracted an INTERVAL value:
SELECT now()::time - INTERVAL '10 min'
If your timestamp field is varchar, you can cast it first to timestamp data type and then subtract the interval:
( (left(precence_records.created::text, 11) || ' ') ||
(contacts.entry_time::time + INTERVAL '10min')::text )::timestamp <
precence_records.created::timestamp
select getdate()- INTERVAL '10 min'
Even if you have to subtract days it can be done; just use " 10 days" instead of minutes.
Related
I need time as duration in postgresql
Eg:- Current time 3/16/2020 13:23:0000
table column entry by 3/15/2020 12:23:0000
so value i need to get is ' 1 day 1 hour ago'
if its like this 3/16/2020 13:20:0000 it should be 3 minutes ago like that
You can do formatting in SQL with something like:
select * from tt;
x
---------------------
2020-02-16 13:30:41
(1 row)
select current_timestamp;
current_timestamp
-------------------------------
2020-03-16 09:38:24.267299+01
(1 row)
select
case when dd > 0 then dd || ' days ' else ' ' end
||
case when hh > 0 then hh || ' hours ' else ' ' end
||
case when mi > 0 then mi || ' minutes ' else ' ' end
||
'ago' as when
from
(
select
extract(day from (current_timestamp - x)) as dd,
extract(hour from (current_timestamp - x)) as hh,
extract(minute from (current_timestamp - x)) as mi
from tt
) as t;
when
--------------------------------
28 days 20 hours 7 minutes ago
(1 row)
You can create a stored function for that:
create or replace function format_timestamp(timestamp) returns text
as
$$
select
case when dd > 0 then dd || ' days ' else ' ' end
||
case when hh > 0 then hh || ' hours ' else ' ' end
||
case when mi > 0 then mi || ' minutes ' else ' ' end
||
'ago' as when
from
(
select
extract(day from (current_timestamp - $1)) as dd,
extract(hour from (current_timestamp - $1)) as hh,
extract(minute from (current_timestamp -$1 )) as mi
) as t;
$$
language sql;
I am trying to set up an SQL query in Firebird 2.5 in order to make a statistic about sales grouped by time.
My approach I followed up so far would be:
SELECT EXTRACT(WEEKDAY FROM ODR.ORDERDATE) AS S1, EXTRACT(HOUR FROM ODR.ORDERDATE) AS S2,
CASE
WHEN (EXTRACT(HOUR FROM ODR.ORDERDATE)) < 10 THEN
'0' || CAST(EXTRACT(HOUR FROM ODR.ORDERDATE) AS VARCHAR(2)) || ':00 - ' || '0' ||
CAST(EXTRACT(HOUR FROM ODR.ORDERDATE) AS VARCHAR(2)) || ':59 '
WHEN EXTRACT(HOUR FROM ODR.ORDERDATE) >= 10 THEN
CAST(EXTRACT(HOUR FROM ODR.ORDERDATE) AS VARCHAR(2)) || ':00 - ' ||
CAST(EXTRACT(HOUR FROM ODR.ORDERDATE) AS VARCHAR(2)) || ':59 '
END AS TINTERVAL,
COUNT(ODR.ID) AS ABSCOUNT
FROM ODR
GROUP BY S1,S2,TINTERVAL
ORDER BY S1,S2 ASC
The syntax of this query seems to be fine for SQL-Connection established by Firebird ODBC 2.5.
My question:
For hour=0 (<10) the query returns a result set which is not presented by the data given by the orders.
e.g. the result-set for this query might look like this:
S1 S2 TINTERVAL ABSCOUNT
0 0 00:00 - 00:59 30
0 1 01:00 - 01:59 2
I don't know how this query comes to its strange count in hour = 0.
This does not make any sense to me.
I'm open to any suggestions, so long as I get something resembling the output at the bottom of the post, TIA!
Can I get some help with this sql fiddle: http://sqlfiddle.com/#!4/c51c5/1
SCHEMA
create table history(
clockingGroup varchar2(5)
, startTime timestamp
, endTime timestamp);
insert into history
(clockingGroup, startTime, endTime)
values
('grp1', '01-dec-2015 1:00:00.000000', '01-dec-2015 1:10:10.000000');
insert into history
(clockingGroup, startTime, endTime)
values
('grp2', '01-dec-2015 1:10:10.000000', '01-dec-2015 1:20:20.000000');
insert into history
(clockingGroup, startTime, endTime)
values
('grp1', '01-dec-2015 1:20:20.000000', '01-dec-2015 1:30:35.000000');
insert into history
(clockingGroup, startTime, endTime)
values
('grp3', '01-dec-2015 1:30:35.000000', '01-dec-2015 1:35:00.000000');
SQL
select
extract(hour from (sum(cast(endTime as date) - cast(startTime as date)))) || ' Hours '
|| extract(minute from (sum(cast(endTime as date) - cast(startTime as date)))) || ' Minutes '
|| extract(second from (sum(cast(endTime as date) - cast(startTime as date)))) || ' Seconds'
as totalTime
, clockingGroup
from
history
group by
clockingGroup
Current Error
ORA-30076: invalid extract field for extract source
My desired output is:
clockingGroup | totalTime
grp1 | 0 Hours 20 Minutes 25 Seconds
grp2 | 0 Hours 10 Minutes 10 Seconds
grp3 | 0 Hours 4 Minutes 25 Seconds
You are using the built ins in the wrong order. Your original query casts your timestamps to dates. When you subtract dates from each other you get a number.
In the query below it subtracts a timestamp from a timestamp which yields an INTERVAL. You cannot extract the hour from a number but you can from an INTERVAL.
SELECT clockingGroup.
SUM(extract(DAY FROM endtime-starttime)) ||' '||
SUM(extract(HOUR FROM endtime-starttime)) ||' '||
SUM(extract(MINUTE FROM endtime-starttime)) ||' '||
SUM(extract(SECOND FROM endtime-starttime)) AS TOTALTIME,
from
history
group by
clockingGroup
I see you have added another method using numtodsinterval. You are still converting timestamps to dates which you don't need to do if you want precision.
This query is more complicated as I try and show how you take the difference and count all the intervals as seconds, sum them and then break it apart into hours/minutes/seconds
SELECT CLOCKINGGROUP, TO_CHAR(TRUNC(SUMTOTALSECONDS/3600),'FM9900') || ' Hours ' ||
TO_CHAR(TRUNC(MOD(SUMTOTALSECONDS,3600)/60),'FM00') || ' Minutes ' ||
TO_CHAR(MOD(SUMTOTALSECONDS,60),'FM00') || ' Seconds'
FROM(
SELECT clockinggroup, TRUNC(SUM(TOTALSECONDS),0) AS SUMTOTALSECONDS
FROM (
SELECT clockinggroup,
EXTRACT (DAY FROM (EndTime-StartTime))*24*60*60 +
EXTRACT (HOUR FROM (EndTime-StartTime))*60*60 +
EXTRACT (MINUTE FROM (EndTime-StartTime))*60 +
EXTRACT (SECOND FROM (EndTime-StartTime))/60 AS TOTALSECONDS
FROM history)
group by
clockingGroup)
ORDER BY 1;
If I want to add 5 days to a date, I can do it using the INTERVAL function:
select create_ts + interval '5 days' from abc_company;
However, my table has a field called num_of_days and I want to add it to my create_ts. Something like this:
select create_ts + interval num_of_days || ' days' from abc_company;
This does not work. How can I accomplish this in postgresql?
Simply multiply the value with an interval:
select create_ts + num_of_day * interval '1' day
from abc_company;
Since Postgres 9.4 this is easier done using the make_interval() function:
select create_ts + make_interval(days => num_of_day)
from abc_company;
You just need a working type cast. This kind is standard SQL.
select current_timestamp + cast((num_of_days || ' days') as interval)
from abc_company;
This is an alternative syntax, peculiar to PostgreSQL.
select current_timestamp + (num_of_days || ' days')::interval
from abc_company;
I prefer not trying to remember the third kind of type cast supported by PostgreSQL, which is the function-like syntax.
select current_timestamp + "interval" (num_of_days || ' days')
from abc_company;
Why? Because some function names have to be quoted; interval is one of them.
Also, the names interval, time, and timestamp can only be used in this
fashion if they are double-quoted, because of syntactic conflicts.
Therefore, the use of the function-like cast syntax leads to
inconsistencies and should probably be avoided.
here is a function that I use:
CREATE OR REPLACE FUNCTION DateAdd(diffType varchar(15), incrementValue int, inputDate timestamp) RETURNS timestamp AS $$
DECLARE
YEAR_CONST Char(15) := 'year';
MONTH_CONST Char(15) := 'month';
WEEK_CONST Char(15) := 'week';
DAY_CONST Char(15) := 'day';
HOUR_CONST Char(15) := 'hour';
dateTemp timestamp;
intervals interval;
BEGIN
IF lower($1) = lower(YEAR_CONST) THEN
select cast(cast(incrementvalue as character varying) || ' year' as interval) into intervals;
ELSEIF lower($1) = lower(MONTH_CONST) THEN
select cast(cast(incrementvalue as character varying) || ' months' as interval) into intervals;
ELSEIF lower($1) = lower(DAY_CONST) THEN
select cast(cast(incrementvalue as character varying) || ' day' as interval) into intervals;
ELSEIF lower($1) = lower(WEEK_CONST) THEN
select cast(cast(incrementvalue as character varying) || ' week' as interval) into intervals;
ELSEIF lower($1) = lower(HOUR_CONST) THEN
select cast(cast(incrementvalue as character varying) || ' hour' as interval) into intervals;
END IF;
dateTemp := inputdate + intervals;
RETURN dateTemp;
END;
$$ LANGUAGE plpgsql;
Used like so:
select dateadd('day', 3, current_timestamp);
It supports adding years, months, weeks, days, hours. More support could be added
Please help me to solve this.
I have a table that contain users check in (checktype = I) and check out (checktype = 0) time everyday, and I would like to get the total amount of check in time per user which occur > 08:00 AM in a specific date range.
I am using the query below, but only handle one day per query not in a range, so I have to loop using javascript to get the amount of delay ( > 08:00 AM) per user for example from 01/06/2012 to 06/06/2012
Please help me to get the amount (count) check in time > 08:00 AM per user (ex: userid 708) from ex:01/06/2012 to 06/06/2012 in a single query.
with tt as
(
select TO_DATE('01/06/2012 08:00:00','dd/mm/yyyy hh24:mi:ss') date1 ,
checktime date2
from
checkinout
where
userid = '708' and
to_char(checktime,'dd/mm/yyyy') = '01/06/2012' and
checktype='I' -- checktype I is check in
) , t2 as
(
select numtodsinterval(date2 - date1,'day') dsinterval from tt
)
select extract(hour from dsinterval) || ' hours ' ||
extract(minute from dsinterval) || ' minutes ' ||
round(extract(second from dsinterval)) || ' seconds' late from t2
I assume you wanted to get how many hours late (i.e. after 08:00) the checkins have been done:
with t2 as (
select userid
,numtodsinterval(sum(checktime - (trunc(checktime)+8/24)),'day') dsinterval
,count(1) cnt
from checkinout
where userid='708'
and checktime > trunc(checktime)+8/24
and trunc(checktime) between to_date('01/06/2012','DD/MM/YYYY') and to_date('06/06/2012','DD/MM/YYYY')
and checktype = 'I'
group by userid
)
select extract(hour from dsinterval) || ' hours ' ||
extract(minute from dsinterval) || ' minutes ' ||
round(extract(second from dsinterval)) || ' seconds' late
,cnt
from t2;
See http://sqlfiddle.com/#!4/c4670/11 for my test case.
edit: added column "cnt" to show how many times
Consider the following example on base of this you can write your own logic
WITH tbl AS
(SELECT SYSDATE dt
FROM DUAL
UNION
SELECT SYSDATE + (1 + (10 / 1440))
FROM DUAL
UNION
SELECT SYSDATE + (2 + (12 / 1440))
FROM DUAL
UNION
SELECT SYSDATE + (3 + (13 / 1440))
FROM DUAL
UNION
SELECT SYSDATE + (6 + (15 / 1440))
FROM DUAL
UNION
SELECT SYSDATE + (8 + (18 / 1440))
FROM DUAL)
SELECT EXTRACT (HOUR FROM dsinterval)
|| ' hours '
|| EXTRACT (MINUTE FROM dsinterval)
|| ' minutes '
|| ROUND (EXTRACT (SECOND FROM dsinterval))
|| ' seconds' late
FROM (SELECT NUMTODSINTERVAL (dt1 - dt2, 'day') dsinterval
FROM (SELECT TO_DATE (TO_CHAR (dt, 'DD/MM/YYYY') || ' 08:00:00',
'DD/MM/YYYY HH24:MI:SS'
) dt1,
TO_DATE (TO_CHAR (dt, 'DD/MM/YYYY HH24:MI:SS'),
'DD/MM/YYYY HH24:MI:SS'
) dt2
FROM tbl
WHERE dt BETWEEN SYSDATE + 2 AND SYSDATE + 5))
As per code you can write like
SELECT EXTRACT (HOUR FROM dsinterval)
|| ' hours '
|| EXTRACT (MINUTE FROM dsinterval)
|| ' minutes '
|| ROUND (EXTRACT (SECOND FROM dsinterval))
|| ' seconds' late
FROM (SELECT NUMTODSINTERVAL (dt1 - dt2, 'day') dsinterval
FROM (SELECT TO_DATE (TO_CHAR (checktime , 'DD/MM/YYYY') || ' 08:00:00',
'DD/MM/YYYY HH24:MI:SS'
) dt1,
TO_DATE (checktime, 'DD/MM/YYYY HH24:MI:SS') dt2
FROM checkinout
WHERE checktime BETWEEN start_date AND end_date
AND checktype='I'))