ERROR: operator does not exist: timestamp without time zone - bigint . - ( a.DURATION - postgresql-9.4

SELECT
a.CARRIER,
b. CARRIER
FROM
V_CARRIER_NOT_MATCH a, V_HGC_NOT_MATCH b
WHERE
a.END_DATE_TIME >= b.END_DATE_TIME
- (a.DURATION
+ (SELECT DISTINCT TIME_ADJ_MINS * 60
FROM TYPE_PARAM
WHERE CARRIER = a.CARRIER))
/ 24
/ 60
/ 60;

Related

Converting SQL query into PostgreSQL

I am trying to convert this SQL Server query below into PostgreSQL...
SQL Server Query:
DECLARE #UTC_OFFSET integer
set #UTC_OFFSET = -4 --EDT -- -5 for EST
select
DATEADD(hour,#UTC_OFFSET, CAST((q.FIRED_TIME - 599266080000000000) / 10000000 / 24 / 60 / 60 AS datetime)) FiredTime
, DATEDIFF(MINUTE,DATEADD(MINUTE,0, CAST((q.FIRED_TIME - 599266080000000000) / 10000000 / 24 / 60 / 60 AS datetime)),Getdate()) as RunTimeMin
, FIRED_TIME
from QRTZ_FIRED_TRIGGERS Q
inner join ScheduledJobs SJ on sj.JobID = Q.JOB_NAME
inner join JobCategories c on c.ID = sj.CategoryID
order by FiredTime desc
Output:
FiredTime RunTimeMin FIRED_TIME
2022-04-20 14:26:07.453 58894 637860759674544643
PostgreSQL Query:
select
to_timestamp((FIRED_TIME - 599266080000000000) / 10000000 / 24 / 60 / 60) as "FiredTime"
,now()-to_timestamp((FIRED_TIME - 599266080000000000) / 10000000 / 24 / 60 / 60 ) as "RunTimeMin"
, FIRED_TIME
from QRTZ_FIRED_TRIGGERS Q
inner join ScheduledJobs SJ on sj.JobID = Q.JOB_NAME
inner join JobCategories c on c.ID = sj.CategoryID
order by FIRED_TIME desc
Query Output:
FiredTime |RunTimeMin |fired_time |
-----------------------------+--------------------------+------------------+
1970-01-01 07:25:10.000 -0500|19143 days 07:42:31.036628|637896173296983621|
1970-01-01 07:25:10.000 -0500|19143 days 07:42:31.036628|637896173288826690|
1970-01-01 07:25:10.000 -0500|19143 days 07:42:31.036628|637896173288762226|
1970-01-01 07:25:10.000 -0500|19143 days 07:42:31.036628|637896173288695965|
Questions:
-The "FiredTime" values are outputting a "1970-01-01 07:25:10.000 -0500" value in Postges, and in SQL Server "2022-04-20". What is causing this issue, and any idea how to resolve?

Why does SELECT '2019-05-03'::timestamp - '2018-05-07'::timestamp < '1 year 1 day'::INTERVAL; return FALSE in postgresql?

I am trying to compare two dates and return TRUE if the first date is less than '1 year 1 day' from the second date.
Using 361 days instead of '1 year 1 day' returns FALSE, but this makes sense based on why justify_interval('360 days'::interval) results '1 year'.
But when I run
SELECT '2019-05-03'::timestamp - '2018-05-07'::timestamp < '1 year 1 day'::INTERVAL;
I get FALSE, and when I run
SELECT '2019-05-03'::timestamp - '1 year 1 day'::INTERVAL < '2018-05-07'::timestamp;
I get TRUE.
Why do these return different things?
I can't find it in the docs, but this is due to the way intervals represented and compared.
Note that:
select timestamp '2019-05-03' - timestamp '2018-05-07' < interval '366 day';
gives you the expected result of TRUE.
To compare two intervals, Postgres first converts the intervals to integers. This is done in a pretty naive way, where years are concerned:
/*
* interval_relop - is interval1 relop interval2
*
* Interval comparison is based on converting interval values to a linear
* representation expressed in the units of the time field (microseconds,
* in the case of integer timestamps) with days assumed to be always 24 hours
* and months assumed to be always 30 days. To avoid overflow, we need a
* wider-than-int64 datatype for the linear representation, so use INT128.
*/
So, your query is asking:
select 361 * 24 * 3600 * 1000000 < (1 * 12 * 30 * 24 * 3600 * 1000000) + (1 * 24 * 3600 * 1000000);
Or,
select 31,190,400,000,000 < 31,190,400,000,000;
which is obviously false. ^^

Generate random values from table

I'd like to generate random values in order to fill a table.
First, I have a city_table :
CREATE TABLE t_city_ci (
ci_id SERIAL PRIMARY KEY,
ci_name VARCHAR(100) NOT NULL
);
So I insert random values like this :
INSERT INTO t_city_ci ("ci_name")
SELECT DISTINCT(d.str)
FROM (
SELECT
(
SELECT string_agg(x, '') as str
FROM (
SELECT chr(ascii('A') + (random() * 25)::integer)
-- reference 'b' so it is correlated and re-evaluated
FROM generate_series(1, 10 + b * 0)
) AS y(x)
)
FROM generate_series(1,10000) as a(b)) as d;
Now, I have a temperature table that looks like this :
CREATE TABLE dw_core.t_temperatures_te (
te_id SERIAL PRIMARY KEY,
ci_id INTEGER,
te_temperature FLOAT NOT NULL,
te_date TIMESTAMP NOT NULL DEFAULT NOW()
);
How can I fill a temperature table with :
Random date from last year
Random temperature between -30 and 50
Random values from t_city table ?
I tried this but the date never changes :
INSERT INTO dw_core.t_temperatures_te ("ci_id","te_temperature","te_date")
SELECT *
FROM (
SELECT (random() * (SELECT MAX(ci_id) FROM dw_core.t_city_ci) + 1)::integer
-- reference 'b' so it is correlated and re-evaluated
FROM generate_series(1, 100000 )
) AS y
,(select random() * -60 + 45 FROM generate_series(1,1005)) d(f),
(select timestamp '2014-01-10 20:00:00' +
random() * (timestamp '2014-01-20 20:00:00' -
timestamp '2016-01-10 10:00:00')) dza(b)
LIMIT 1000000;
Thanks a lot
Something like this?
select * from (
select
(random() * 100000)::integer as ci_id,
-30 + (random() * 80) as temp,
'2014-01-01'::date + (random() * 365 * '1 day'::interval) as time_2014
from generate_series(1,1000000) s
) foo
inner join t_city_ci c on c.ci_id = foo.ci_id;
Here's a sample of the generated data:
select
(random() * 100000)::integer as ci_id,
-30 + (random() * 80) as temp,
'2014-01-01'::date + (random() * 365 * '1 day'::interval) as time_2014
from generate_series(1,10);
ci_id | temp | time_2014
-------+-------------------+----------------------------
84742 | 31.6278865475337 | 2014-10-16 21:36:45.371176
16390 | 10.665458049935 | 2014-11-13 19:59:54.148177
87067 | 43.2082599369847 | 2014-06-01 16:14:43.021094
25718 | -7.78245567240867 | 2014-07-23 05:53:10.036914
99538 | -5.82924078024423 | 2014-06-08 06:44:02.081918
71720 | 22.3102275898262 | 2014-06-15 08:24:00.327841
24740 | 4.65809369210996 | 2014-05-19 02:20:58.804213
56861 | -20.750980894431 | 2014-10-01 06:09:54.117367
47929 | -24.4018202994027 | 2014-11-24 13:39:54.096337
30772 | 46.7239395141247 | 2014-08-27 04:50:46.785239
(10 rows)

Postgres convert integer into time

I need to convert the following (hh)integer column into a time column. Expected results below:
hh time
1 00:00
2 00:30
3 01:00
4 01:30
...
48 23:30
Can you help?
You can just do:
select hh, ('00:00:00'::time + (hh - 1) * interval '30 minute') as time
from t;
Use INTERVAL arithmetic
SELECT
TIME '00:00' + (1 - 1) * INTERVAL '30 minutes',
TIME '00:00' + (2 - 1) * INTERVAL '30 minutes',
TIME '00:00' + (3 - 1) * INTERVAL '30 minutes',
TIME '00:00' + (4 - 1) * INTERVAL '30 minutes',
TIME '00:00' + (5 - 1) * INTERVAL '30 minutes',
-- ...
TIME '00:00' + (48 - 1) * INTERVAL '30 minutes'
Another solution would be to work with unix timestamps:
SELECT
(to_timestamp((1 - 1) * 30 * 60) at time zone 'UTC')::time,
(to_timestamp((2 - 1) * 30 * 60) at time zone 'UTC')::time,
(to_timestamp((3 - 1) * 30 * 60) at time zone 'UTC')::time,
(to_timestamp((4 - 1) * 30 * 60) at time zone 'UTC')::time,
(to_timestamp((5 - 1) * 30 * 60) at time zone 'UTC')::time,
-- ...
(to_timestamp((48 - 1) * 30 * 60) at time zone 'UTC')::time

current time minus mod_date_time

I want to be able to subtract the current time from the mod_date_time field.
This is what I have so far:
SELECT * FROM PROD_TRKG_TRAN.MOD_DATE_TIME - CURRENT_TIME WHERE USER_ID = '12345' * 24 * 60 "INTERVAL IN MINUTES"
Any help?
If you are using Oracle, you can use the following query:
SELECT (MOD_DATE_TIME - SYSDATE) * 24 * 60
FROM PROD_TRKG_TRAN
WHERE USER_ID = '12345';