1

I'm stuck in get the total count of group by query result.

JPAQuery jpaQuery = new JPAQuery(entityManager);
jpaQuery.from(QMessageSend.messageSend)
        .where(predicate)
        .groupBy(QMessageSend.messageSend.messageId).count();

this is my current code.
and this code return count for each group.

SELECT count(*)
FROM MESSAGE_SEND ms
GROUP BY ms.MESSAGE_ID

but I want to get the total count of group by query result. like result of below sql

SELECT count(*)
FROM (
   SELECT *
   FROM MESSAGE_SEND ms
   GROUP BY ms.MESSAGE_ID
) msc

What I should do?

MESSAGE_ID is not primary key. and JPAQuery's distinct method not support select specific column. (has no parameter)

vmaldosan
  • 444
  • 4
  • 14
chooco13
  • 53
  • 1
  • 9

1 Answers1

0

I think you want COUNT(DISTINCT):

SELECT COUNT(DISTINCT s.MESSAGE_ID)
FROM MESSAGE_SEND ms;

If MESSAGE_ID is the primary key in the table, then COUNT(*) suffices.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • MESSAGE_ID is not primary key. and JPAQuery distinct method not supporting select column. ;( – chooco13 Nov 18 '17 at 13:11
  • @chooco13 . . . I noticed the focus on JPA after I answered. Does this answer the question? https://stackoverflow.com/questions/6197591/how-to-do-a-distinct-count-in-jpa-critera-api. – Gordon Linoff Nov 18 '17 at 13:57