I want to specify three time intervals, am I doing this correctly?
where
(time_created between '2022-06-27 13:00:00' and '2022-06-27 20:00:00'
or
time_created between '2022-06-28 13:00:00' and '2022-06-28 20:00:00'
or
time_created between'2022-06-29 13:00:00' and '2022-06-29 20:00:00')
Related
My data looks like the following:
amount(double precision) time (timestamp without timezone)
3.53456532 "2021-03-29 09:41:09.052+00"
3.77389602 "2021-03-28 23:42:15.413+00"
3.77389602 "2021-03-28 23:42:10.176+00"
3.77389602 "2021-03-28 23:42:02.589+00"
3.77389602 "2021-03-28 23:41:57.226+00"
3.05223612 "2021-03-28 20:12:51.457+00"
21.55 "2021-03-28 18:50:35.174+00"
7.98374607 "2021-03-28 09:30:31.698+00"
What I would like to achieve is a select which would return me the following:
amount(double precision) time (timestamp without timezone)
3.53456532 "2021-03-29 00:00:00.000+00"
47.68156627 "2021-03-28 00:00:00.000+00"
So the total amount per 24h, I tried the following:
SELECT time as "time",
amount
FROM trades
GROUP BY DAY(time)
But I have then following issue:
pq: function day(timestamp with time zone) does not exist
I tried many alternatives but I am a bit stuck, any help? thanks!
There is no day() function in PostgreSQL. Simply cast the time column to a date and aggregate on that.
select time::date as time,
sum(amount) as amount
from trades
group by time::date
Please note: The values for 03/28 in your example sums to 47.68156627 instead of 45.91297.
SELECT cu.user_id, cu.last_activity, cu.updated_time,
DATE_PART('day', cu.last_activity - cu.updated_time), to_char(end_date - start_date, 'DD.HH24')
FROM stats.core_users cu
WHERE cu.user_id = '117132014' or cu.user_id = '117132012';
Get the result like:
117132014 2017-12-11 10:34:51.349905 2017-12-09 12:00:38.503518 1 01.22
117132012 2017-12-11 05:18:20.312283 2017-12-08 15:46:51.914085 2 02.13
Is is feasible to get the day difference with fractions like 1.91 days in the first case, instead of 1 days and 22 hours, to be more precise and easier to fit in a machine learning model?
date_part() does what it's name says: it returns one part of several elements from a date, interval or timestamp. In your case it's one part of an interval (because timestamp - timestamp returns an interval).
If you want the result as a fraction, you need to extract the seconds of the interval and then divide that by 86400 (which is the number of seconds in a day)
extract(epoch from cu.last_activity - cu.updated_time) / 86400
I am having table with three columns and may have duplicate data in it. what I am trying to do is if BATCH Column is having Duplicate Data then START_S and END_S should be according to below Example
CREATE TABLE "DRL_FTO3_DI1_A0_BATCH"
(
"BATCH" character varying(128),
"START_S" integer,
"END_S" integer
)
INSERT INTO "DRL_FTO3_DI1_A0_BATCH"(
"BATCH", "START_S", "END_S")
VALUES ('Batch 1_1',1451120920,1451121008),
('Batch 01_2',1451389014,1451389100),
('Batch 2_1',1451534680,1451534918),
('Batch 3_1',1451539145,1451539264),
('Parth_2',1451540990,1451541285),
('Parth_2',1451541676,1451542254)
SELECT "BATCH",((TIMESTAMP WITHOUT Time Zone 'epoch' + "START_S" * INTERVAL '1 second') AT TIME ZONE 'UTC')::TIMESTAMP WITHOUT Time Zone,
((TIMESTAMP WITHOUT Time Zone 'epoch' + "END_S" * INTERVAL '1 second') AT TIME ZONE 'UTC')::TIMESTAMP WITHOUT Time Zone
FROM "DRL_FTO3_DI1_A0_BATCH"
Now as we can see Parth_2 is duplicate value so START_S and END_S for Parth_S should be
Parth_2 2015-12-31 11:19:50 2015-12-31 11:40:54
You could do it using GROUP BY and MIN/MAX aggregate functions (you can convert into date time later with below query in format you desire) like:
SELECT BATCH, MIN(START_S), MAX(END_S)
FROM DRL_FTO3_DI1_A0_BATCH
GROUP BY BATCH
I have a list of events and each one has a startDate and endDate. I need to know the average time taken for each event.
I need something like this:
select sum ( (timestamp(startDate) - timestamp(endDate)) for each event )
/ (count of events)
It only makes mathematical sense to take the AVG() of a numeric value, not datetime values or durations. Since you want your answer to be in minutes precision, you want to get your difference in minutes, then convert back to days, hours, minutes. (There are 24*60=1440 minutes in a standard day.)
with q as
(select avg(
timestampdiff(4, char(endDate - startDate) )
) as avgmns
from yourChosenData
)
select int(avgmns / 1440) as avg_days,
int( mod(avgmns,1440) / 60) as avg_mins,
mod(avgmns, 60) as avg_secs
from q
As mentioned below, timestampdiff() is an estimate. To avoid this issue, one could use a more accurate calculation.
with q as
(select avg(
( days(endDate) - days(startDate) ) * 1440
+ ( midnight_seconds(endDate) - midnight_seconds(startDate) ) / 60
) as avgmns
from yourChosenData
)
select int(avgmns / 1440) as avg_days,
int( mod(avgmns,1440) / 60) as avg_mins,
mod(avgmns, 60) as avg_secs
from q
In order to address the DST issue, if needed, one might choose either of:
include a UTC offset column corresponding to each timestamp field. This would also be useful if timstamps were being recorded in more than one timezone. The diference in offsets could then be fed into the calculation along with the timestamps.
provide a deterministic UDF which could return a UTC or DST adjustment offset for a given timestamp. If multiple timezones are involved, then the zone should also be a parameter to the function. Depending on the geographic areas involved, the logic may also need to consider areas which observe alternative DST rules.
You have to be careful of the denominator to prevent a 0 division: SQL0802 - Data Conversion or Data Mapping Error
Depending on the precision of the results, you will need to convert the date. Let's suppose you need seconds (2)
select
sum ( timestampdiff(2, endDate - startDate))
/
sum (count of events)
from yourTable
http://pic.dhe.ibm.com/infocenter/db2luw/v10r5/topic/com.ibm.db2.luw.sql.ref.doc/doc/r0000861.html
I have a sql query that is very slow:
select number1 from mytable
where symbol = 25
and timeframe = 1
and date::date = '2008-02-05'
and date::time='10:40:00' + INTERVAL '30 minutes'
The goal is to return one value, and postgresql takes 1.7 seconds to return the desired value(always a single value). I need to execute hundreds of those queries for one task, so this gets extremely slow.
Executing the same query, but pointing to the time directly without using interval and ::date, ::time takes only 17ms:
select number1 from mytable
where symbol = 25
and timeframe = 1
and date = '2008-02-05 11:10:00'
I thought it would be faster if I would not use ::date and ::time, but when I execute a query like:
select number1 from mytable
where symbol = 25
and timeframe = 1
and date = '2008-02-05 10:40:00' + interval '30 minutes'
I get a sql error (22007). I've experimented with different variations but I couldn't get interval to work without using ::date and ::time. Date/Time Functions on postgresql.org didn't help me out.
The table got a multi column index on symbol, timeframe, date.
Is there a fast way to execute the query with adding time, or a working syntax with interval where I do not have to use ::date and ::time? Or do I need to have a special index when using queries like these?
Postgresql version is 9.2.
Edit:
The format of the table is:
date = timestamp with time zone,
symbol, timeframe = numeric.
Edit 2:
Using
select open from ohlc_dukascopy_bid
where symbol = 25
and timeframe = 1
and date = timestamp '2008-02-05 10:40:00' + interval '30' minute
Explain shows:
"Index Scan using mcbidindex on mytable (cost=0.00..116.03 rows=1 width=7)"
" Index Cond: ((symbol = 25) AND (timeframe = 1) AND (date = '2008-02-05 11:10:00'::timestamp without time zone))"
Time is now considerably faster: 86ms on first run.
The first version will not use a (regular) index on the column named date.
You didn't provide much information, but assuming the column named date has the datatype timestamp (and not date), then the following should work:
and date = timestamp '2008-02-05 10:40:00' + interval '30 minutes'
this should use an index on the column named date (but only if it is in fact a timestamp not a date). It is essentially the same as yours, the only difference is the explicit timestamp literal (although Postgres should understand '2008-02-05 10:40:00' as a timestamp literal as well).
You will need to run an explain to find out if it's using an index.
And please: change the name of that column. It's bad practise to use a reserved word as an identifier, and it's a really horrible name, which doesn't say anything about what kind of information is stored in the column. Is it the "start date", the "end date", the "due date", ...?