I have the following problem retrieving messages from the OpenFire Monitoring Service plugin. I discovered that the error is due to an incorrect query in the database but I still cannot detect the error for which the query is not working correctly.
SELECT
fromjid,
fromjidresource,
tojid,
tojidresource,
sentdate,
body,
stanza,
messageid,
barejid
FROM
(
SELECT
DISTINCT ofmessagearchive.fromjid,
ofmessagearchive.fromjidresource,
ofmessagearchive.tojid,
ofmessagearchive.tojidresource,
ofmessagearchive.sentdate,
ofmessagearchive.body,
ofmessagearchive.stanza,
ofmessagearchive.messageid,
ofconparticipant.barejid
FROM
ofmessagearchive
INNER JOIN ofconparticipant ON ofmessagearchive.conversationid =
ofconparticipant.conversationid
WHERE
(
ofmessagearchive.stanza IS NOT NULL
OR ofmessagearchive.body IS NOT NULL
)
AND ofmessagearchive.messageid IS NOT NULL
AND ofmessagearchive.sentdate >= 0
AND ofmessagearchive.sentdate <= 1602748770287
AND ofconparticipant.barejid = 'usuario3#192.168.0.79'
AND (
ofmessagearchive.tojid = 'usuario4#192.168.0.79'
OR ofmessagearchive.fromjid = 'usuario3#192.168.0.79'
)
ORDER BY
ofmessagearchive.sentdate DESC
LIMIT
100
) AS part
ORDER BY
sentdate
I get an error when doing the following query
ORA-00907: missing right parenthesis
Command line error:32 Column: 9
There is no LIMIT keyword available in Oracle and if you are using Oracle 12c you can use FETCH FIRST 100 ROWS ONLY instead of it.
You cannot use AS to give alias to the sub query and it is not recognised by Oracle. So either you can remove the alias completely as you are not using it anywhere or just remove the AS and keep the alias name part only which should be fine.
Here is a good SO link about the Oracle limiting result set and you can always look into other sites available such as Oracle base or the official document as well. For 11g solution you have to use row_number
Related
I'm working on spring boot project where I am trying to run following query on both sql client and spring boot code.
SELECT two.STORE_NBR, two.TLE_WORK_ORDER_ID,
tj.SERVICE_START_TS, tj.SERVICE_END_TS, tj.WIN_NBR
FROM TLE_WORK_ORDER AS two
INNER JOIN TLE_TECHNICIAN_JOB AS tj
ON ((two.TLE_WORK_ORDER_ID = tj.TLE_WORK_ORDER_ID))
WHERE CREATION_TIMESTAMP LIKE '%2017-11-13%'
LIMIT 5 OFFSET 2;
The data type of CREATION_TIMESTAMP is string.
This query is throwing an error.
Can someone explain that what is the issue with this query?
The query is throwing following error:
org.jkiss.dbeaver.model.sql.DBSQLException: SQL Error [102] [S0001]: Incorrect syntax near 'LIMIT'.
at org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl.executeStatement(JDBCStatementImpl.java:133)
at org.jkiss.dbeaver.ui.editors.sql.execute.SQLQueryJob.executeStatement(SQLQueryJob.java:577)
at org.jkiss.dbeaver.ui.editors.sql.execute.SQLQueryJob.lambda$1(SQLQueryJob.java:486)
at org.jkiss.dbeaver.model.exec.DBExecUtils.tryExecuteRecover(DBExecUtils.java:172)
at org.jkiss.dbeaver.ui.editors.sql.execute.SQLQueryJob.executeSingleQuery(SQLQueryJob.java:493)
at org.jkiss.dbeaver.ui.editors.sql.execute.SQLQueryJob.extractData(SQLQueryJob.java:894)
at org.jkiss.dbeaver.ui.editors.sql.SQLEditor$QueryResultsContainer.readData(SQLEditor.java:3710)
at org.jkiss.dbeaver.ui.controls.resultset.ResultSetJobDataRead.lambda$0(ResultSetJobDataRead.java:123)
at org.jkiss.dbeaver.model.exec.DBExecUtils.tryExecuteRecover(DBExecUtils.java:172)
at org.jkiss.dbeaver.ui.controls.resultset.ResultSetJobDataRead.run(ResultSetJobDataRead.java:121)
at org.jkiss.dbeaver.ui.controls.resultset.ResultSetViewer$ResultSetDataPumpJob.run(ResultSetViewer.java:4949)
at org.jkiss.dbeaver.model.runtime.AbstractJob.run(AbstractJob.java:105)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near 'LIMIT'.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:262)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1632)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.doExecuteStatement(SQLServerStatement.java:872)
at com.microsoft.sqlserver.jdbc.SQLServerStatement$StmtExecCmd.doExecute(SQLServerStatement.java:767)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7418)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:3274)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:247)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:222)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.execute(SQLServerStatement.java:743)
at org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl.execute(JDBCStatementImpl.java:329)
at org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl.lambda$0(JDBCStatementImpl.java:131)
at org.jkiss.dbeaver.utils.SecurityManagerUtils.wrapDriverActions(SecurityManagerUtils.java:96)
at org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl.executeStatement(JDBCStatementImpl.java:131)
... 12 more
Since you're using SQL-Server, you can't use LIMIT <N> OFFSET <M> notation. You should rather use OFFSET FETCH, that uses the ORDER BY clause to determine the order of the rows, before truncating your result set.
In your specific query, you seem to be using "tj.SERVICE_START_TS", "tj.SERVICE_END_TS" as two timestamp fields, to be used in the ORDER BY clause.
Your final query should look like:
SELECT two.STORE_NBR, two.TLE_WORK_ORDER_ID,
tj.SERVICE_START_TS, tj.SERVICE_END_TS, tj.WIN_NBR
FROM TLE_WORK_ORDER AS two
INNER JOIN TLE_TECHNICIAN_JOB AS tj
ON ((two.TLE_WORK_ORDER_ID = tj.TLE_WORK_ORDER_ID))
WHERE CREATION_TIMESTAMP LIKE '%2017-11-13%'
ORDER BY tj.SERVICE_START_TS, tj.SERVICE_END_TS
OFFSET 2 ROWS
FETCH NEXT 5 ROWS ONLY;
Recommendation: It's a bad habit to use VARCHAR datatypes for date fields, in place of the DATE, DATETIME or TIMESTAMP fields. SQL-Server operates faster and gives you tool to deal with dates.
I have a following table:
EstimatedCurrentRevenue -- Revenue column value of yesterday
EstimatedPreviousRevenue --- Revenue column value of current day
crmId
OwnerId
PercentageChange.
I am querying two snapshots of the similarly structured data in Azure data lake and trying to query the percentage change in Revenue.
Following is my query i am trying to join on OpportunityId to get the difference between the revenue values:
#opportunityRevenueData = SELECT (((opty.EstimatedCurrentRevenue - optyPrevious.EstimatedPreviousRevenue)*100)/opty.EstimatedCurrentRevenue) AS PercentageRevenueChange, optyPrevious.EstimatedPreviousRevenue,
opty.EstimatedCurrentRevenue, opty.crmId, opty.OwnerId From #opportunityCurrentData AS opty JOIN #opportunityPreviousData AS optyPrevious on opty.OpportunityId == optyPrevious.OpportunityId;
But i get the following error:
E_CSC_USER_SYNTAXERROR: syntax error. Expected one of: AS EXCEPT FROM
GROUP HAVING INTERSECT OPTION ORDER OUTER UNION UNION WHERE ';' ')'
','
at token 'From', line 40
near the ###:
This expression is having the problem i know but not sure how to fix it.
(((opty.EstimatedCurrentRevenue - optyPrevious.EstimatedPreviousRevenue)*100)/opty.EstimatedCurrentRevenue)
Please help, i am completely new to U-sql
U-SQL is case-sensitive (as per here) with all SQL reserved words in UPPER CASE. So you should capitalise the FROM and ON keywords in your statement, like this:
#opportunityRevenueData =
SELECT (((opty.EstimatedCurrentRevenue - optyPrevious.EstimatedPreviousRevenue) * 100) / opty.EstimatedCurrentRevenue) AS PercentageRevenueChange,
optyPrevious.EstimatedPreviousRevenue,
opty.EstimatedCurrentRevenue,
opty.crmId,
opty.OwnerId
FROM #opportunityCurrentData AS opty
JOIN
#opportunityPreviousData AS optyPrevious
ON opty.OpportunityId == optyPrevious.OpportunityId;
Also, if you are completely new to U-SQL, you should consider working through some tutorials to establish the basics of the language, including case-sensitivity. Start at http://usql.io/.
This same crazy sounding error message can occur for (almost?) any USQL syntax error. The answer above was clearly correct for the provided code.
However since many folks will probably get to this page from a search for 'AS EXCEPT FROM GROUP HAVING INTERSECT OPTION ORDER OUTER UNION UNION WHERE', I'd say the best advice to handle these is look closely at the snippet of your code that the error message has marked with '###'.
For example I got to this page upon getting a syntax error for a long query and it turned out I didn't have a casing issue, but just a malformed query with parens around the wrong thing. Once I looked more closely at where in the snippet the ### symbol was, the error became clear.
I have the query below
set hive.cli.print.header=true;
set hive.query.max.partition=1000;
set hive.mapred.mode=unstrict;
SELECT
dim_lookup("accounts",name,"account_id") = '28016' as company,
dim_lookup("campaigns",name,"campaign_id") in (117649,112311,112319,112313,107799,110743,112559,112557,105191,105231,107377,108675,106587,107325,110671,107329,107181,106565,105123,106569,106579,110835,105127,105243,107185,105211,105215) as campaign_name,
case when is_click_through=0 then "PV" else "PC" end as conv_type,
(SELECT COUNT(1) FROM impressions WHERE ad_info[2] in (117649,112311,112319,112313,107799,110743,112559,112557,105191,105231,107377,108675,106587,107325,110671,107329,107181,106565,105123,106569,106579,110835,105127,105243,107185,105211,105215)) AS impressions
FROM actions
WHERE
data_date>='20170101'
AND data_date<='20171231'
AND conversion_action_id in (20769223,20769214,20769219,20764929,20764932,20764935,20769215,20769216,20764919,20769218,20769217,20769220,20769222)
GROUP BY conv_type
When I execute it I get an error
ERROR ql.Driver: FAILED: ParseException line 8:1 cannot recognize input near 'SELECT' 'COUNT' '(' in expression specification
I am trying to fetch each count of impression for a specified conversion_action_id. What could be the error in my query? Thanks for the help.
FYI: ad_info[2] and campaign_id are the same.
The problem is quite clear, you have a subquery inside your SELECT.
That is not how this works.
Unfortunately the exact solution is not that clear, as it I am not completely sure what you want, but here is some general advice:
Write your subquery, test it and make sure it is ok
Rather than putting it in your SELECT part, put it in your FROM part, and (as always) SELECt from the FROM
Just think of your subquery output as an other table that can be used in the from statement, and which needs to be combined (JOIN, UNION?) with other tables in the from statement.
I am running a SQL query in stored procedure which is like following
SELECT
t1.id,t2.Name
FROM
table1 t1 , table2 t2 ,table2 t3,table4 t4
WHERE
t1.id=t3.t4.id
this query gets executed on SQL server 2008 when its compatible with SQL server 2000 but if we turn OFF the compatibility with SQL server 2000 then this Query gives syntax error which is expected.
Can some one help me to understand why this is happeneing ? thanks in advance
Original query:
SELECT
ConfigID , LocationDesc + '-' + LOBTeamDesc LocLOBTeamSource
FROM Config CONFIG , Location_LOBTeam LOCLOB , Location LOC , LOBTeam LOB, System SRC
WHERE CONFIG.LocationLOBTeamID = LOC.LOB.LocationLOBTeamID
AND CONFIG.SourceSystemID = SRC.SystemID
AND LOCLOB.LocationID = LOC.LocationID
AND LOCLOB.LOBTeamID = LOB.LOBTeamID
AND (GETDATE() BETWEEN CONFIG.effectiveDate AND CONFIG.EndDate
OR CONFIG.EndDate IS NULL)
ORDER BY
LOC.LocationCode
I think that original query, with current standard join syntax applied would be this:
SELECT
ConfigID
, LocationDesc + '-' + LOBTeamDesc LocLOBTeamSource
FROM Config CONFIG
INNER JOIN Location_LOBTeam LOCLOB
ON CONFIG.LocationLOBTeamID = LOCLOB.LocationLOBTeamID
INNER JOIN Location LOC
ON LOCLOB.LocationID = LOC.LocationID
INNER JOIN LOBTeam LOB
ON LOCLOB.LOBTeamID = LOB.LOBTeamID
INNER JOIN [System] SRC
ON CONFIG.SourceSystemID = SRC.SystemID
WHERE (GETDATE() BETWEEN CONFIG.effectiveDate AND CONFIG.EndDate
OR CONFIG.EndDate IS NULL)
ORDER BY
LOC.LocationCode
Perhaps this will help.
+EDIT
"System" as a table name, could that be a problem? Suggest you try it as [System]
+EDIT2
The original is given with this: LOC.LOB.LocationLOBTeamID but that appears to be an error as there is an alias LOCLOB
I think below post from msdn answers this issue Compatibility Levels and Stored Procedures
in the above post the point number 3 under section "Differences Between Compatibility Level 80 and Level 90" states "WHEN binding the column references in the ORDER BY list to the columns defined in the SELECT list, column ambiguities are ignored and column prefixes are sometimes ignored. This can cause the result set to return in an unexpected order."
on my database I am using compatibility level 80 i.e 2000 thats why it runs smoothly with the given syntax but when I remove this compatibility and make it to 100 i.e. 2008/R2 script gives syntax error which is expected
I am using Oracle Client 10.2g and by changing my connection string to the Oracle database I now get error ORA00907 for some of my queries.
The code is executing within excel 2010 using VBA and I can run 20+ quires without error using the following connection string:
ServerConnectionString="Driver={Oracle in OraClient10g_home1};Dbq=DBNAME;Uid=USERNAME;Pwd=PASSWORD;"
With OpenR2DBConnection
.ConnectionTimeout = ConnectionTimeout
.Open ServerConnectionString
.Execute "ALTER SESSION SET NLS_DATE_FORMAT = 'DD/MM/YYYY'"
.CommandTimeout = CommandTimeout
End With
By changing the connection string only to:
ServerConnectionString="Provider=OraOLEDB.Oracle;Data Source=(<<<data exact translation from TNSNAMES file for the DBNAME>>>);User id=USERNAME;Password=PASSWORD;"
2 of the 20+ queries fail with ORA-00907: missing right parenthesis
One of the Queries that fails (refracted):
select n1.name,n1.sdate,n1.edate,n1.note as crnote,n1.cdate ,n1.pri
from
(
select n.name,n.sdate,n.edate,n.note,n.cdate,n.pri , RANK() OVER (PARTITION BY n.sdate,pri ORDER BY (n.adate - n.cdate) asc,n.pri asc) RANK
from
(
select mmpe.name,mmpe.sdate,mmpe.edate,mmpe.cannote as note,mmpe.cdate,mmip.crdate as adate,mp.pri
from mmpe ,mmmip mmip, mp
where <<clauses1>>
and mmpe.name in (<<list of strings>>)
and mmip.name(+) = mmpe.name
and <<more clauses2>>
union
Select mmip.name,mmip.sdate,greatest(<<formula>>) as edate , <<create note>> as note ,mmip.crdate as cdate, td.adate,mp.pri
From mi , mmip,td, mp
Where <<clauses3>>
and mi.name in (<<list of strings>>)
union all
Select mmip.name,mmip.sdate,mmip.edate as edate , <<create note>> as note ,mmip.crdate as cdate , td.adate,mp.pri
From mi , mmip,td, mp
Where <<clauses4>>
and mi.name in (<<list of strings>>)
union
Select mmip.name,mmip.sdate,td.cddate-1 as edate , <<create note>> as note, mmip.crdate as cdate,mmip.crdate as adate,mp.pri
From mi , mmip,td, mp
Where <<clauses4>>
and mi.name in (<<list of strings>>)
) n
) n1
where rank = 1
order by 6,2,5;
I have tested that the Query runs correctly in Oracle SQL Developer.
I have verified that prior to Executing the query the SQL statements are identical for both connection strings.
The other query that fails is also using a union and rank function but it is not the only one.
The reason I wish to use the OraOLEDB.Oracle connection is that I am attempting to remove my reliance on the tnsnames.ora files as from time to time I add new database instances and want to avoid all my users having to update this file in the oracle directory.
Lastly the ORACLE database is version 8.
Any help would be greatly appreciated,
Thanks in advance!
Updated: Removed typo error
OK I've work it out.
Thanks to all those that spent any time thinking about it.
When I refracted the example above I removed inline comments denoted by '--'.
It was these comments that caused the ORA-00907 error with the OraOLEDB.Oracle driver.
So the fix is simple: Remove comments from the SQL command!
Thanks again!