I've a SQL query (2 variants) and I need to use it with dynamic queries.
In SQL it looks so (Variant 1 // via subquery):
SELECT AssetEntry.entryId , (
SELECT COUNT(*)
FROM `MBMessage`
WHERE classPK = AssetEntry.classPK
) AS comments
FROM `AssetEntry`
ORDER BY comments DESC
Or the alternative query with join and group:
SELECT AssetEntry.entryId, count(MBMessage.classPK)
FROM `AssetEntry`
JOIN MBMessage ON (AssetEntry.classPK = MBMessage.classPK)
GROUP BY MBMessage.classPK
Both SQL Queries displays exactly the same!
Now I need to use one of them as dynamic query. I have no idea how to do a join and I have no idea how to do a subquery in the projection?!
Can anybody help me? THX
I had to do this with custom-sql.
Your requirement is a really specific case, which requires an aggregate-function in the
SELECTstatement.I would suggest using Custom query (also known as
custom-sqlin liferay) with finders in your case, instead of aDynamicQuery.DynamicQuery API has limitations and will not work in your case (speaking from experience, so if somebody else has other opinions or facts regarding the below points I would be more than happy to know):
countor to return an individual columns value usingProjectionbut not possible to return a column and count to-gather using projections.selectstatement with DynamicQuery.