Count with distinct
The scenario is: I have many inventories that are associated with a single tree (Many to One).
I have a query that selects the inventories from the database.
obs: This is a partial query; there are more than 300 lines of select and where clauses.
I use distinct along with orderBy to ensure I only get the latest inventory from each tree, not a random one.
What is the problem?
After I get the inventories, I need to count them for pagination purposes.
The only way I found to count them is to select every inventory in the database grouped by treeId and use .length() to count.
Is there any easier way to perform this count? I thought of something like COUNT DISTINCT, but I can't use raw queries because I have 295 lines of conditional where clauses.
1 Reply
repost*