Spring data r2dbc and group by

1.5k views Asked by At

I am using the DatabaseClient to perform my sql queries and I don't see how I can do a group by:

databaseClient.select()
              .from( myClass.class )
              .matching( where( "row_1" ).is( "value_1" ) )
              //group by row_2 ...
              .orderBy( Sort.Order.desc( "row_3" ) )
              .page( pageable  )
              .fetch()
              .all();
1

There are 1 answers

0
Hantsy On

A workaround solution using sql directly.


    public Flux<Map<Object, Object>> countByStatus() {
        return this.databaseClient
                .sql("SELECT count(*) as cnt, status FROM posts group by status")
                .map((row, rowMetadata) -> {
                    Long cnt = row.get("cnt", Long.class);
                    Post.Status s = row.get("status", Post.Status.class);

                    return Map.<Object, Object>of("cnt", cnt, "status", s);
                })
                .all();
    }

Check the complete codes.