简体   繁体   中英

How can I return date and count of records for that date using spring data jpa?

I want to return event count by date.Below is my code

@Query(value = "select DATE(e.startTime) as day,count(distinct e.id) as eventCount from events e " +
            "where e.clientId=:clientId and e.startTime>=:firstDay and e.startTime<=:lastDay group by DATE(e.startTime)",nativeQuery = true)
    Map<Integer, Integer> getClientEventsCountByDay(@Param(value = "clientId") Integer clientId,
                                                    @Param(value = "firstDay") String firstDay,
                                                    @Param(value = "lastDay") String lastDay);

Input is clientId=2510,firstDay=2019-01-01,lastDay=2019-01-31

This query is throwing below error

Caused by: org.springframework.dao.IncorrectResultSizeDataAccessException: result returns more than one elements; nested exception is javax.persistence.NonUniqueResultException: result returns more than one elements

However this query is working fine when running directly on db. 在此处输入图片说明

Try changing to this

List<Map<Integer, Integer>> getClientEventsCountByDay(@Param(value = "clientId") Integer clientId,
                                                @Param(value = "firstDay") String firstDay,
                                                @Param(value = "lastDay") String lastDay);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM