Ranjeet Singh
Ranjeet Singh

Reputation: 61

JPA @Query how to use count, distinct & concat together version

JPA version 1.11.22

Inside a @Query how can we use count, distinct & concat together.
Basically I want the distinct count of a group of multiple columns.

I've tried the following queries

@Query("SELECT count(distinct concat(col1, col2, col3)) from entity where col1 is not null")

Thanks

Upvotes: 1

Views: 632

Answers (1)

Raja
Raja

Reputation: 21

I tried with work around for this and it worked. Try to add this to your entity @Formula (from org.hibernate.annotations) like below,

@Formula("concat(col1, col2, col3)") private String concated;

and in spring repository, try with @Query("SELECT count(distinct concated) from entity where col1 is not null")

Upvotes: 1

Related Questions