how to manage hour interval in hive, I try this code:
select DATE_SUB(current_timestamp(),INTERVAL '1' HOUR);
error return : Error while compiling statement: FAILED: ParseException line 1:124 cannot recognize input near 'INTERVAL' ''1'' 'HOUR' in select expression
Unfortunately, that is not how date_sub() works in Hive. And unfortunately, it does not simply support interval arithmetic.
So, unix formats to the rescue!
select from_unixtime(unix_timestamp(current_timestamp()) - 3600)
Of course, you don't need to do the conversion to unix time for the current date/time:
select from_unixtime(unix_timestamp() - 3600)
Related
When I execute my query I have this error :
Query failed: ERROR: operator does not exist: "GOOD_RECIEPT_ANOMALY_DETECTION_V2_last_alerts_sent_gsheet_histo" > timestamp without time zone Hint: No operator matches the given name and argument types. You might need to add explicit type casts. Position: 176
Here is my SQL query :
SELECT *
FROM "GOOD_RECIEPT_ANOMALY_DETECTION_V2_last_alerts_sent_gsheet_historic"
WHERE "GOOD_RECIEPT_ANOMALY_DETECTION_V2_last_alerts_sent_gsheet_historic.PO_Creation_date" >
CURRENT_DATE - INTERVAL '1 month'
I work on PostgreSQL database.
I think the problem comes from the PO_Creation_date type but I don't understand that it should be cast since it is in date format
use ::DATE
SELECT *
FROM "GOOD_RECIEPT_ANOMALY_DETECTION_V2_last_alerts_sent_gsheet_historic"
WHERE "GOOD_RECIEPT_ANOMALY_DETECTION_V2_last_alerts_sent_gsheet_historic"."PO_Creation_date"::DATE > CURRENT_DATE - INTERVAL '1 month'
I am running a query against Athena, and it breaks. Specifically, I get an error for the below fragment:
avg(
DATE_DIFF(
'minute',
CAST(from_iso8601_timestamp("sessions_staging".session_start_at) AS TIMESTAMP),
CASE
WHEN CAST("sessions_staging__end_raw" AS TIMESTAMP) + INTERVAL '1' MINUTE > CAST("sessions_staging".next_session_start_at AS TIMESTAMP) THEN CAST("sessions_staging".next_session_start_at AS TIMESTAMP)
ELSE CAST("sessions_staging__end_raw" AS TIMESTAMP) + INTERVAL '30' MINUTE
END
)
) "sessions_staging__average_duration_minutes"
Athena complains with Value cannot be cast to timestamp: 2022-08-03T00:05:54.300Z.
I tried a bunch of tricks like casting my date to string then casting again to a time or a timestamp type. A similar problem caused by the same issue is covered some in converting to timestamp with time zone failed on Athena
The value seems to be just fine. I am able to execute: SELECT CAST(From_iso8601_timestamp('2022-08-03T00:05:54.300Z') AS timestamp). If I do not use CAST() and just do: "sessions_staging".session_start_at, it says that (varchar(6), varchar, timestamp) for function date_diff so I know that session_start_at is perceived as VARCHAR.
However, for the type of casting described as a solution to my issue to work, in the linked discussion, SELECT need to be used, it seems. Everything that I tried including string manipulations did not work.
How could I re-write my query/casts for Athena to process my request?
I ended up with:
CAST(DATE_PARSE(my_varchar_date, '%Y-%m-%dT%H:%i:%s.%f%z') AS TIMESTAMP)
I have a query like the following
CAST(kokyaku1Information2.mail_jyushin as integer) as information2_mail_jyushin,
(date '$mytime' - INTERVAL 'information2_mail_jyushin' day) AS modified_date,
When run the query i get an error like 'invalid input syntax for type interval'. I used another select field named information2_mail_jyushin before day.
In Postgres, you would use interval arithmetics like this:
kyaku1Information2.mail_jyushin::int AS information2_mail_jyushin,
date '$mytime'
- kokyaku1Information2.mail_jyushin::int * interval '1 day'
AS modified_date
Note that concatenating variables in a SQL statement is bad practice, and opens up your code to SQL injection attacks. Instead, use parameters, as in:
$1::date
- kokyaku1Information2.mail_jyushin::int * interval '1 day'
AS modified_date
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
I am trying to use a parameterized query in a Perl script to get some timestamps back from a Postgres database. Here's a cut-and-dried example, solely for pedagogical purposes.
I've defined $start_date and $end_date as timestamps and intervals:
my $start_date = "current_timestamp - interval '6 hours'";
my $end_date = "current_timestamp";
I use the following to submit to the database, with $dbh defined earlier:
my $sql = "SELECT cast(? as timestamp), cast(? as timestamp)";
my $sth = $dbh->prepare($sql);
$sth->execute($start_date, $end_date);
When I do this, I get a somewhat confusing error.
DBD::Pg::st execute failed: ERROR: date/time value "current" is no longer supported
I understand that current hasn't been supported in PG since 7.2, but I'm not using that. I'm using current_timestamp, which is supported, AFACT. To wit, if I enter into psql:
select (cast(current_timestamp - interval '6 hours' as timestamp), cast(current_timestamp as timestamp));
the result is what I expect (two timestamps, the former six hours previous to the latter).
I could also use now() rather than current_timestamp. I can use it in the following way:
my $start_date = "now() - interval '6 hours'";
my $end_date = "now()";
When I try to run the query in perl, I get the following error:
DBD::Pg::st execute failed: ERROR: invalid input syntax for type timestamp: "now() - interval '6 hours'"
Yet, the query:
select (cast(now() - interval '6 hours' as timestamp), cast(now() as timestamp));
gives me the expected result.
I am quite flummoxed.
The problem is that a SQL placeholder doesn't represent an expression, but a single value. And that value can't be a function. You could do something like:
my $start_date = "6 hours";
my $sql = "SELECT current_timestamp - cast(? as interval), current_timestamp";
my $sth = $dbh->prepare($sql);
$sth->execute($start_date);
What you're doing in Perl is equivalent to doing this in psql:
select (cast('current_timestamp - interval ''6 hours''' as timestamp), cast('current_timestamp' as timestamp));
To make the windows of your queries a bit more flexible:
$sth = $dbh->prepare(<<__eosql);
SELECT * FROM tbl
WHERE ts BETWEEN current_timestamp - ? * CAST('1 ' || ? AS INTERVAL)
AND
current_timestamp;
__eosql
$sth->execute(6, 'hour');
$sth->execute(10, 'day');
$sth->execute(1, 'week');
# etc.
When you introduce fixed time points, you could do something too clever like ... WHERE COALESCE(?, current_timestamp) ... and remember that an undef parameter defaults to the current time. However, I'd probably write and prepare a separate query.