简体   繁体   English

如何在 jooq 中编写此查询

[英]How to write this query in jooq

select sum(x.amt) from (select amount as amt from user_transactions where TransactionTimeStamp > '2022-03-09') as x

I am not able to convert this sql query to jooq query.我无法将此 sql 查询转换为 jooq 查询。 Please help请帮忙

Creating derived tables创建派生表

You can create derived tables like this:您可以像这样创建派生表

// Assuming this static import, as always:
import static org.jooq.impl.DSL.*;

Table<?> x = table(
    select(USER_TRANSACTIONS.AMOUNT)
    .from(USER_TRANSACTIONS)
    .where(USER_TRANSACTIONS.TRANSACTIONTIMESTAMP.gt(
        LocalDate.parse("2022-03-09")
    ))
).as("x");

And then use it like this:然后像这样使用它:

ctx.select(sum(x.field(USER_TRANSACTIONS.AMOUNT)))
   .from(x)
   .fetch();

Alternative using FILTER使用FILTER的替代方法

jOOQ supports the standard SQL FILTER clause, and can emulate it also for SQL Server: jOOQ 支持标准的 SQL FILTER子句,也可以为 SQL 服务器模拟它:

ctx.select(sum(USER_TRANSACTIONS.AMOUNT).filterWhere(
       USER_TRANSACTIONS.TRANSACTIONTIMESTAMP.gt(LocalDate.parse("2022-03-09"))
   ))
   .from(USER_TRANSACTIONS)
   .fetch();

This is especially useful if you're aggregating several things in a single query , eg with different filters如果您在单个查询中聚合几件事,例如使用不同的过滤器,这将特别有用

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

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