简体   繁体   English

无法编写 jooq 查询

[英]Unable to write jooq query

I am not able to write above query in JOOQ .我无法在 JOOQ 中编写上述查询。

Here is my postgres query这是我的 postgres 查询

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 个地方发现困难 1) 将子查询的结果分配给临时表
2) Getting last 30 days result : current_date - interval '30' day 2) 获取过去 30 天的结果:current_date - 间隔 '30' 天

1) Assigning result of subquery to temp table 1) 将子查询的结果分配给临时表

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:鉴于 jOOQ 是 Java 中的内部 DSL,您不能直接在使用它的地方内联派生表,但必须在声明之前预先声明它:

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:现在您可以加入temp表并使用以下方法从中提取列:

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

2) Getting last 30 days result : current_date - interval '30' day 2) 获取过去 30 天的结果:current_date - 间隔 '30' 天

Day intervals can be created easily by subtracting integers from your date columns, eg通过从日期列中减去整数,可以轻松创建天间隔,例如

currentDate().minus(30)

Or using an interval:或者使用间隔:

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

Of course, you can also use DSL.dateSub()当然,你也可以使用DSL.dateSub()

dateSub(currentDate(), 30)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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