jan
jan

Reputation: 4063

JPA - Only select object for first day of month

I am collecting daily datasets like this:

date        value
01.01.2017  5
02.01.2017  6
03.01.2017  10
.
.
.
01.02.2017  30
02.02.2017  33
.
.
.
01.03.2017  50

So one entry per day. Then I use this to draw some chart. Now if the data gets to much, I want to draw a monthly chart instead and therefore only select the first object for the month. E.g. the result should be:

date        value
01.01.2017  5
01.02.2017  30
01.03.2017  50

Is there a way to get this result with JPQL? The result should be contain entity objects.

Upvotes: 1

Views: 430

Answers (1)

Neil Stockton
Neil Stockton

Reputation: 11531

Assuming (big assumption maybe?, but then you need to make the question clearer to get precise replies) you have an entity like this

@Entity
public class Dataset
{
    @Id
    long id;

    Date date;

    long value;
}

Then you can execute JPQL such as

Query q = em.createQuery("SELECT d FROM Dataset d WHERE DAY(d.date) == 1");

While DAY is not standard JPQL (yet) it is supported by the majority of JPA providers out there.

Upvotes: 2

Related Questions