jooq query for sql 具有分析功能的查询
jooq query for sql query with analytical function
我有 sql 个类似
的查询
select *
from (
select s.*, max(year) over () max_year
from Transaction s
where s.Account_Number = '4234242234'
and s.month in (0,1)) o
where o.year=o.max_year;
我需要用 jOOQ 编写,任何帮助。
我尝试了以下
dslContext.select(Transaction.BANK,
Transaction.ACCOUNT,
Transaction.AMOUNT,
max(Transaction.YEAR).over())
.from(Transaction)
.where(Transaction.BANK.eq(bank))
.and(Transaction.MONTH.in((byte) 0, (byte) 1))
.fetchInto(Transaction.class);
此外,有什么方法可以避免列出 select 中的所有列。而不是
select(Transaction.BANK,
Transaction.ACCOUNT,
Transaction.AMOUNT,
max(Transaction.YEAR).over())
类似
select(Transaction.*,
max(Transaction.YEAR).over())
派生的table可以这样构造:
// Assuming this static import, as always:
import static org.jooq.impl.DSL.*;
// Create aliased column expression for later reuse
Field<Integer> maxYear = max(Transaction.YEAR).over().as("max_year");
// Create derived table
Table<?> o = select(Transaction.asterisk(), maxYear)
.from(Transaction)
.where(Transaction.BANK.eq(bank))
.and(Transaction.MONTH.in((byte) 0, (byte) 1))
.asTable("o");
// Use derived table and dereference fields from it using Table.field(Field)
ctx.select()
.from(o)
.where(o.field(Transaction.YEAR).eq(o.field(maxYear)))
.fetch();
Also, is there any way to avoid listing all columns in select
您正在寻找 Table.asterisk()
to produce the asterisk in SQL, or Table.fields()
从您生成的代码中生成所有字段。
我有 sql 个类似
的查询select *
from (
select s.*, max(year) over () max_year
from Transaction s
where s.Account_Number = '4234242234'
and s.month in (0,1)) o
where o.year=o.max_year;
我需要用 jOOQ 编写,任何帮助。 我尝试了以下
dslContext.select(Transaction.BANK,
Transaction.ACCOUNT,
Transaction.AMOUNT,
max(Transaction.YEAR).over())
.from(Transaction)
.where(Transaction.BANK.eq(bank))
.and(Transaction.MONTH.in((byte) 0, (byte) 1))
.fetchInto(Transaction.class);
此外,有什么方法可以避免列出 select 中的所有列。而不是
select(Transaction.BANK,
Transaction.ACCOUNT,
Transaction.AMOUNT,
max(Transaction.YEAR).over())
类似
select(Transaction.*,
max(Transaction.YEAR).over())
派生的table可以这样构造:
// Assuming this static import, as always:
import static org.jooq.impl.DSL.*;
// Create aliased column expression for later reuse
Field<Integer> maxYear = max(Transaction.YEAR).over().as("max_year");
// Create derived table
Table<?> o = select(Transaction.asterisk(), maxYear)
.from(Transaction)
.where(Transaction.BANK.eq(bank))
.and(Transaction.MONTH.in((byte) 0, (byte) 1))
.asTable("o");
// Use derived table and dereference fields from it using Table.field(Field)
ctx.select()
.from(o)
.where(o.field(Transaction.YEAR).eq(o.field(maxYear)))
.fetch();
Also, is there any way to avoid listing all columns in select
您正在寻找 Table.asterisk()
to produce the asterisk in SQL, or Table.fields()
从您生成的代码中生成所有字段。