spark.sql("""SELECT CAST('22:00:00' AS INTERVAL HOUR TO SECOND)""").show()
I am executing the above query but getting the below error
ERROR : ParseException:
missing ')' at 'HOUR'(line 1, pos 35)
INTERVAL HOUR TO SECOND sql syntax format:
select INTERVAL '22:00:00' HOUR TO SECOND
Related
I want to write the following query in a phoenix application using Ecto fragments:
select *
from (
select id,
inserted_at + interval '1 day' * expiry as deadline
from friend_referral_code
) t
where localtimestamp at time zone 'UTC' > deadline
The value of expiry is an integer value that represents number of days. What I've got so far is something like this:
query = from frc in FriendReferralCode,
where: fragment("localtimestamp at time zone 'UTC'") > fragment("? + INTERVAL '1' * ?", frc.inserted_at, frc.expiry)
FriendReferralCode
|> Repo.all(query)
|> Enum.each(fn frc -> update_friend_referral_code_users(get_friend_referral_code_users_by(receiver_id: frc.id), %{status: false}) end)
|> IO.puts()
end
but it throws the following error:
** (EXIT) an exception was raised:
** (FunctionClauseError) no function clause matching in Keyword.merge/2
(elixir 1.11.2) lib/keyword.ex:764: Keyword.merge([], #Ecto.Query<from f0 in Stakester.Accounts.FriendReferralCode, where: fragment("localtimestamp at time zone 'UTC'") > fragment("? + INTERVAL '1 day' * ?", f0.inserted_at, f0.expiry)>)
You are after Ecto.Query.API.ago/2 and Ecto.Query.API.from_now/2 for querying interval and Ecto.Query.subquery/2 for inner select.
Also, Repo.all/2 expects a query as a first argument, while you pass FriendReferralCode as the first argument in the call to Repo.all/2, where it expects a query, and query as a second one, where it expects a keyword list of options.
Do just query |> Repo.all() instead.
I am trying to translate following sql query into knex:
select count(*) as finished_on_time from task_history
where date = 20160303
and store_id = 2
and (schedule_start_time at time zone 'Australia/sydney' + interval '1' minute * floor (group_duration) )::time >= (finish_time at time zone 'Australia/sydney')::time
date field has in yyyymmdd format
Here is what I have been trying on knex:
db.table('task_history')
.count('*')
.where({date: request.params.storeid, store_id: request.params.storeid })
??????
As you can guess, I am not sure which clause to use to handle sql syntax [at time zone Australia/sydney].
I have been trying to find any similar soloutions on the internet, but ended up here.
http://knexjs.org/#Builder-whereRaw
db.table('task_history')
.count('*')
.where({date: request.params.storeid, store_id: request.params.storeid })
.whereRaw("(schedule_start_time at time zone 'Australia/sydney' + interval '1' minute * floor (group_duration) )::time >= (finish_time at time zone 'Australia/sydney')::time")
I am trying to retrieve time difference in minutes from a table(login_history as t1) using postgresql .
When i tried this code
((date_part('hour', timestamp '2014-04-25 09:44:21')- date_part('hour', timestamp '2014-04-25 08:32:21'))*60 +(date_part('minutes', timestamp '2014-04-25 09:44:21')- date_part('minutes', timestamp '2014-04-25 08:32:21'))) as TimeNew
It works fine.
But when i tried to retrieve information from a table t1 using this code
((date_part('hour', timestamp t1.login_date)- date_part('hour', timestamp t1.logout_date))*60 +
(date_part('minutes', timestamp t1.login_date)- date_part('minutes', timestamp t1.logout_date))
) as TimeNew
It throws this error
SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "t1"
Thanks
I would use the interval that results from subtracting two timestamps for a much simpler expression:
select extract (epoch from (timestamp '2014-04-25 09:44:21' - timestamp '2014-04-25 08:32:21'))::integer/60
(gives 72)
or for your table:
select extract (epoch from (t1.logout_date - t1.login_date))::integer/60
If you need to cast:
select extract (epoch from (t1.logout_date::timestamp - t1.login_date::timestamp))::integer/60
or see the to_timestamp function for custom string parsing: http://www.postgresql.org/docs/9.4/static/functions-formatting.html
I needed to remove the timestamp from the query before t1 and the query works.
I'm trying to query record filtering on the date (documentation for date)
select * from InstallationFee where infinite OR (date() >= dateFrom and date() <= dateTo)
The query returns the following error in the studio:
java.lang.ClassCastException: com.orientechnologies.orient.core.sql.filter.OSQLFilterItemField cannot be cast to com.orientechnologies.orient.core.sql.filter.OSQLFilterCondition
Here is the stack trace in the JAVA api
com.orientechnologies.orient.core.exception.OCommandExecutionException: Error on execution of command: sql.select * from InstallationFee where (infinite OR date() >= dateFrom and date() <= dateTo)
at com.orientechnologies.orient.core.storage.impl.local.OAbstractPaginatedStorage.executeCommand(OAbstractPaginatedStorage.java:1190)
at com.orientechnologies.orient.core.storage.impl.local.OAbstractPaginatedStorage.command(OAbstractPaginatedStorage.java:1173)
at com.orientechnologies.orient.core.sql.query.OSQLQuery.run(OSQLQuery.java:71)
at com.orientechnologies.orient.core.sql.query.OSQLSynchQuery.run(OSQLSynchQuery.java:85)
at com.orientechnologies.orient.core.query.OQueryAbstract.execute(OQueryAbstract.java:33)
at com.orientechnologies.orient.server.network.protocol.binary.ONetworkProtocolBinary.command(ONetworkProtocolBinary.java:1178)
at com.orientechnologies.orient.server.network.protocol.binary.ONetworkProtocolBinary.executeRequest(ONetworkProtocolBinary.java:385)
at com.orientechnologies.orient.server.network.protocol.binary.OBinaryNetworkProtocolAbstract.execute(OBinaryNetworkProtocolAbstract.java:216)
at com.orientechnologies.common.thread.OSoftThread.run(OSoftThread.java:65)
Caused by: java.lang.ClassCastException: com.orientechnologies.orient.core.sql.filter.OSQLFilterItemField cannot be cast to com.orientechnologies.orient.core.sql.filter.OSQLFilterCondition
at com.orientechnologies.orient.core.sql.OFilterAnalyzer.analyzeUnion(OFilterAnalyzer.java:195)
at com.orientechnologies.orient.core.sql.OFilterAnalyzer.analyzeOrFilterBranch(OFilterAnalyzer.java:80)
at com.orientechnologies.orient.core.sql.OFilterAnalyzer.analyzeMainCondition(OFilterAnalyzer.java:58)
at com.orientechnologies.orient.core.sql.OCommandExecutorSQLSelect.searchForIndexes(OCommandExecutorSQLSelect.java:1454)
at com.orientechnologies.orient.core.sql.OCommandExecutorSQLSelect.searchInClasses(OCommandExecutorSQLSelect.java:765)
at com.orientechnologies.orient.core.sql.OCommandExecutorSQLResultsetAbstract.assignTarget(OCommandExecutorSQLResultsetAbstract.java:194)
at com.orientechnologies.orient.core.sql.OCommandExecutorSQLSelect.assignTarget(OCommandExecutorSQLSelect.java:438)
at com.orientechnologies.orient.core.sql.OCommandExecutorSQLSelect.executeSearch(OCommandExecutorSQLSelect.java:420)
at com.orientechnologies.orient.core.sql.OCommandExecutorSQLSelect.execute(OCommandExecutorSQLSelect.java:391)
at com.orientechnologies.orient.core.sql.OCommandExecutorSQLDelegate.execute(OCommandExecutorSQLDelegate.java:64)
at com.orientechnologies.orient.core.storage.impl.local.OAbstractPaginatedStorage.executeCommand(OAbstractPaginatedStorage.java:1184)
I tried a different approaches in the studio, but all resulted with the error presented above. Here are a few:
Using sysdate
select * from InstallationFee where infinite OR (sysdate("yyyy-MM-dd") >= dateFrom and sysdate("yyyy-MM-dd") <= dateTo)
Using between (documentation)
select * from InstallationFee where infinite OR date("yyyy-MM-dd") between dateFrom and dateTo)
Am I missing something? or Is there a way around this problem?
I believe the error comes from:
... where infinite ...
It should be:
... where infinite = true ...
I want to add word Week to show as Week then number from function MOD i tried the following
select EVENTTIMESTAMP, Year (EVENTTIMESTAMP) as Year, QUARTER (EVENTTIMESTAMP) as Quarter, 'Week' MOD(WEEK(EVENTTIMESTAMP)-1, 13) + 1 as WeekNoQuarter
but i get error :
An unexpected token "MOD" was found following ") as Quarter, 'Week'". Expected tokens may include: ",".. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.16.53
any suggestion on way to put word Week.
Thanks!
In your code you are trying to name a string 'Week' as MOD(WEEK(EVENTTIMESTAMP)-1, 13) + 1 as WeekNoQuarter. Try concatenating ( || ) the String 'Week ' with the calculated value. Since the calculated value is of type int you will have to cast it:
'Week ' || cast(MOD(WEEK(EVENTTIMESTAMP)-1, 13) + 1 as char(2)) as WeekNoQua...