sushma
sushma

Reputation: 153

Unable to write jooq query

I am not able to write above query in JOOQ .

Here is my postgres query

select aps.agent_id,aps.stats_date from application as aps
    join (
        select max(stats_date) as stats_date, agent_id
        from  application
        group by application.agent_id, date(application.stats_date)) temp
    on temp.stats_date = aps.stats_date and temp.agent_id = aps.agent_id and aps.stats_date > current_date - interval '30' day;

Mainly finding difficult in 2 places 1) Assigning result of subquery to temp table
2) Getting last 30 days result : current_date - interval '30' day

Upvotes: 1

Views: 84

Answers (1)

Lukas Eder
Lukas Eder

Reputation: 220762

1) Assigning result of subquery to temp table

Given that jOOQ is an internal DSL in Java, you cannot inline your derived table directly where you're using it, but you'll have to declare it up front, before your statement:

Table<?> temp = table(
  select(
    max(APPLICATION.STATS_DATE).as(APPLICATION.STATS_DATE),
    APPLICATION.AGENT_ID
  )
  .from(APPLICATION)
  .groupBy(APPLICATION.AGENT_ID, date(APPLICATION.STATS_DATE));

Now you can join the temp table and extract columns from it using:

Field<Date> tempStatsDate = temp.field(APPLICATION.STATS_DATE);

2) Getting last 30 days result : current_date - interval '30' day

Day intervals can be created easily by subtracting integers from your date columns, e.g.

currentDate().minus(30)

Or using an interval:

currentDate().minus(new DayToSecond(30))

Of course, you can also use DSL.dateSub()

dateSub(currentDate(), 30)

Upvotes: 1

Related Questions