My query runs fine in SQL Developer, but when I copy the same query into the mapper XML file for MyBatis, I get an ORA-00900: invalid SQL statement.
Subset of mapper file:
<select id="getTotalUniqueUsersByPage">
resultType="KeyValuePair"
parameterType="RequestFilter">
select KEY as key,sum(single_value) as value from (
select schcm.title as key
,schuc.usage_count AS single_value
from usage_count schuc
inner join content_master schcm
on schcm.trace_id=schuc.trace_key
where schcm.pagetype='Page'
)
GROUP by KEY
order by value desc
</select>
### The error occurred while setting parameters
### SQL: resultType="KeyValuePair" parameterType="RequestFilter"> select KEY as key,sum(single_value) as value from ( select schcm.title as key ,schuc.usage_count AS single_value from usage_count schuc inner join content_master schcm on schcm.trace_id=schuc.trace_key where schcm.pagetype='Page' ) GROUP by KEY order by value desc
### Cause: java.sql.SQLSyntaxErrorException: ORA-00900: invalid SQL statement
For the most part, regular SQL that runs in SQL Developer also runs when directly pasted into a MyBatis mapper file. Well, mostly anyway. I did remember to check for a semicolon at the end, but didn't make that mistake this time. What I'd like is for the MyBatis call to work like the SQL Developer call.
Update: Tried
<select id="getTotalUniqueUsersByPage">
resultType="KeyValuePair"
parameterType="RequestFilter">
select 'SomeTitle' as "Key", 55 as "Value" from dual
</select>
but that call fails as well with the same "ORA-00900: invalid SQL statement" In deference to comments below, I surrounded the alias names so that the reserved word "Key" should work, and indeed it works find in SQLDeveloper.
... from (select ...) <ALIAS> GROUP by KEY.... I can't imagine that SQL would work anywhere.keyis a reserved keyword. You need to quote itselect "KEY" as "key" ...