Syntax error near "from" in amazon redshift query - sql

I am working on queries in Amazon RedShift.
I have created in query in which I am getting syntax error but I am unable to know the issue behind it. The query looks OK to me.
Below is the query:
select (TIMESTAMP WITHOUT TIME ZONE 'epoch' + (_action_date::float / 1000000) * INTERVAL '1 second') as eta
where eta >= (SELECT NOW() - INTERVAL '1 DAY')
from trips;
Here, _action_date is a bigint integer so I am converting it into timestamp format also.
I am trying to extract those rows in which _action_date is greater than or equal to yesterday date.
The error I am getting:
Error running query: syntax error at or near "from" LINE 1: ...ta where eta >= (SELECT NOW() - INTERVAL '1 DAY') from trips... ^
Please help me in correcting it. Any help would be highly appreciated.

select (TIMESTAMP WITHOUT TIME ZONE 'epoch' + (_action_date::float / 1000000) *
INTERVAL '1 second') as eta
from trips
where eta >= (SELECT getdate() - INTERVAL '1 DAY');

Related

SQL SELECT WHERE datetime is between NOW() interval

how can I select in MariaDB between two dates (in this case between two minutes)? I mean I want to select between from now() +5 minutes and now() + 30 minutes.
I tried with this query but no luck.
SELECT req_id FROM info WHERE date(sent_date) BETWEEN (NOW() - INTERVAL 5 MINUTE) AND date(sent_date) < (NOW() - INTERVAL 30 MINUTE)
Thank you so much for helping.
BTW, I tried to search answer to my question in stackoverflow but I don't found.
The syntax of your WHERE clause is off. Use this version:
SELECT req_id
FROM info
WHERE sent_date BETWEEN NOW() - INTERVAL 5 MINUTE AND NOW() - INTERVAL 30 MINUTE;

trunc(add_months(sysdate,3),'Q')-1 equivalent expression in Postgres

can anybody convert this oracle expression trunc(add_months(sysdate,3),'Q')-1) to postgresql?
Basically this expression gives you the last day of the current quarter (provided that you remove the last closing parenthese, which otherwise is a syntax error).
In postgres, you could phrase this as:
date_trunc('quarter', current_date) + interval '3 months' - interval '1 day'
This generates a timestamp value, that you can cast if you want a date (this distinction does not exist in Oracle, where a date stores the time component as well).
The Postgres equivalent of your Oracle calculation can be seen below.
select date_trunc('quarter', current_date + interval '3 month') - interval '1 day'

DataType interval is not supported - Spark SQL

I am running a query on AWS EMR and the query errors out on this line -
to_date('1970-01-01', 'YYYY-MM-DD') + CAST(concat(mycolumn, ' seconds') AS INTERVAL) AS date_col
The error - DataType interval is not supported.(line 521, pos 82)
Can someone help me with this?
I think Spark supports the interval key word. It would be used as:
to_date('1970-01-01', 'YYYY-MM-DD') + mycolumn * interval '1 second' AS date_col

SQL Invalid operation for DateTime or Interval when casting a hour/minute to an existing Timestamp

I'm trying to cast an existing Date variable as a timestamp, and add hours and minutes from another Time variable to get a final variable of the format mm/dd/yyyy hh:mm:00.
The current line of the query that errors out is:
cast(DepDt as timestamp) + cast(substr(ArrTm, 1, 2) as interval hour) + cast(substr(ArrTm, 3, 2) as interval minute) as Arrv_DTML
I can't seem to find what's wrong though. I have gotten rid of the substring functions to make sure it wasn't something wrong with that, but I can't seem to cast the ArrTm as an interval even on its own. Is it something with the format of the variables? I am running this in Teradata.
DepDt is a Date. ArrTM is a Time variable.
You can't apply substr on a Time, you must explicitly cast it to a VarChar first:
Cast(DepDt AS TIMESTAMP(0))+
+ Cast(Substr(Cast(ArrTm AS VARCHAR(8)), 1, 5) AS INTERVAL HOUR TO MINUTE)
But there's an easier way to get your result:
Cast(DepDt AS TIMESTAMP(0)) -- date to Timestamp
+ (Extract(HOUR From ArrTm) * INTERVAL '1' HOUR) -- hour to Interval
+ (Extract(MINUTE From ArrTm) * INTERVAL '1' MINUTE) -- minute to Interval

Not abe to declare as timestamp in postgreSQL

select *
From #####
where username in ('akhil') and between (now() ::timestamp and now() - interval '1 day'::timestamp)
Getting error in this line .
Error - cannot cast type interval to timestamp without time zone .
You don't need the second cast. You need a column name. Something like this:
select *
from #####
where username in ('akhil') and
<some date column> >= now() - interval '1 day' and
<some date column> < now();
the error is in the second cast
now() - interval '1 day'::timestamp
is interprested as.
now() - (interval '1 day')::timestamp
when you actually mean
(now() - interval '1 day')::timestamp
or possibly you mean
now()::timestamp - interval '1 day'
both are valid, but the the result you'll get when daylight saving starts or stops is different (hint interval '1 day' is the same as interval '24 hours' in the first)
There's a strong possiblity that you should actually be using timestamp with time zone for the database column.