I want to do something like this in OrmLite
SELECT *, COUNT(title) as titleCount from table1 group by title;
Is there any way to do this via QueryBuilder without the need for queryRaw?
I want to do something like this in OrmLite
SELECT *, COUNT(title) as titleCount from table1 group by title;
Is there any way to do this via QueryBuilder without the need for queryRaw?
The documentation states that the use of COUNT() and the like necessitates the use of
selectRaw()
. I hoped for a way around this - not having to write my SQL as strings is the main reason I chose to use ORMLite.http://ormlite.com/docs/query-builder
Further information on the use of
selectRaw()
as I was attempting much the same thing:Documentation states that if you use
selectRaw()
it will "turn the query into" one that is supposed to be called byqueryRaw()
.What it does not explain is that normally while multiple calls to
selectColumns()
orselectRaw()
are valid (if you exclusively use one or the other), use ofselectRaw()
afterselectColumns()
has a 'hidden' side-effect of wiping out anyselectColumns()
you called previously.I believe that the ORMLite documentation for
selectRaw()
would be improved by a note that its use is not intended to be mixed withselectColumns()
.ORMLite examples are not as plentiful as I'd like, so here is a complete example of something that works:
The short answer is no because ORMLite wouldn't know what to do with the extra count value. If you had a
Table1
entity with a DAO definition, what field would theCOUNT(title)
go into? Raw queries give you the power to select various fields but then you need to process the results.With the code right now (v5.1), you can define a custom
RawRowMapper
and then use thedao.getRawRowMapper()
method to process the results forTable1
and tack on thetitleCount
field by hand.I've got an idea how to accomplish this in a better way in ORMLite. I'll look into it.