Could anybody clarify to me if is it possible accomplishing the following sql statement (MVC example) from within the Dynamic Query world:
select count(*) from <sub_select> where aggregate_field >= J;
<sub_select>::
(select case
when condition1 then 1
when condition2 then 2
...
when conditionN then N
else 0
end as aggregate_field
from Entity) as select1
J={1..N} // J can range 1 to N
The thing is I have to calculate the number of rows satisfiying the where-condition aggregate_field >= J, but the field aggregate_field doesn't belong to the table Entity, but it's calculated depending on some conditions.
There's no problem to create a DynamicQuery object to execute the sub_select:
subQuery = EntityLocalServiceUtil.dynamicQuery();
subQuery.setProjection(ProjectionFactoryUtil.sqlProjection("case when ... end as aggregate_field", new String[] { "aggregate_field" }, new Type[] { Type.INTEGER });
EntityLocalServiceUtil.dynamicQuery(subQuery);
But I don't know how to do to create a DynamiQuery object to get the number of rows of the table linked to subQuery satisfiying the where-condition.
Any ideas about how to accomplish the intended task will be appreciate.
In that case it's better to write a custom sql query. DynamicQuery maybe it's not the right way.
Look at https://help.liferay.com/hc/en-us/articles/360018179071-Developing-Custom-SQL-Queries