简体   繁体   English

使用JOOQ的两个字段的别名

[英]Alias with two Fields using JOOQ

i have a nasty SQl that i want transform in JOOQ 我有一个讨厌的SQl,我想在JOOQ中进行转换

Here are the Query: 这是查询:

  SELECT 
    SUM(dpr.dpr_bruttopraemie_prt + dpr.dpr_sofortrabatt_prt) 
    , MAX(TO_NUMBER(DP1.dp_wert))  
      FROM deckungen deck, deckungspraemien dpr, 
        (SELECT dp.dp_id, dp.dp_wert 
              FROM  textbausteine txb, druckparameter dp 
              WHERE  dp.dp_txb_id = txb.txb_id 
              ) DP1 
      WHERE DP1.dp_id = :druckparameter_id;

As you can see, i need to make alias from a select with two Fields. 如您所见,我需要从具有两个字段的选择中创建别名。

dp.dp_id, dp.dp_wert 

that im going to used it on some other parts. 我将在其他一些地方使用它。

How i can i get it done? 我如何完成它?

i've seen 我见过

.asField() 

Funktion but it only make alias for one column. 功能,但它只为一列做别名。

PS: The actual Query are a lot more complicated. PS:实际查询要复杂得多。 So i wrote a simpler one. 所以我写了一个简单的。 With hoping that it's satisfied the SQL ORACLE Dialect. 希望它满足SQL ORACLE方言。

I'm assuming that you're using the code generator, so you have generated objects available for your tables like DECKUNGEN . 我假设您正在使用代码生成器,因此您已经生成了可用于表的对象,如DECKUNGEN I'm also assuming you're using these static imports: 我还假设您正在使用这些静态导入:

import static org.jooq.impl.DSL.*;                  // The jOOQ API
import static com.example.myapp.generated.Tables.*; // Your generated tables

You can then write: 然后,您可以编写:

Deckungen deck = DECKUNGEN.as("deck");
Deckungspraemien dpr = DECKUNGSPRAEMIEN.as("dpr");
Textbausteine txb = TEXTBAUSTEINE.as("txb");
Druckparameter dp = DRUCKPARAMETER.as("dp");

Table<?> dp1 = table(
    select(dp.DP_ID, dp.DP_WERT)
   .from(txb, dp)
   .where(dp.DP_TXB_ID.eq(txb.TXB_ID))
).as("dp1");

Record2<BigDecimal, BigDecimal> result =
using(configuration)
  .select(
     sum(dpr.DPR_BRUTTOPRAEMIE_PRT.plus(dpr.DPR_SOFORTRABATT_PRT)),
     max(field("to_number({0})", BigDecimal.class, dp1.field(dp.DP_WERT))))
  .from(deck, dpr, dp1)
  .where(dp1.field(dp.DP_ID).eq(druckparameterId))
  .fetchOne();

Some explanations 一些解释

  • jOOQ currently doesn't have a built-in TO_NUMBER() function, but you can easily roll your own using DSL.field(String) and similar overloads. jOOQ当前没有内置的TO_NUMBER()函数,但是您可以使用DSL.field(String)和类似的重载轻松地自己滚动。 For more detail, refer to the manual's section about plain SQL 有关更多详细信息, 请参见手册中有关纯SQL的部分
  • A derived table can be created most easily by using the DSL.table(Select) operator. 使用DSL.table(Select)运算符可以最轻松地创建派生表。
  • Columns from a derived table can be dereferenced using Table.field() methods, in particular the Table.field(Field) , which tries to find a field by the same name as the argument field, retaining the argument field's type information. 可以使用Table.field()方法,特别是Table.field(Field)来取消引用派生表中的列,该方法尝试查找与自变量字段同名的字段,并保留自变量字段的类型信息。

Side-note 边注

I don't think your query is correct as you're simply creating cartesian products between your tables deck , dpr , and dp1 . 我认为您的查询不正确,因为您只是在表deckdprdp1之间创建笛卡尔dp1 Specifically, the SUM() is quite likely to be wrong, whereas MAX() is simply calculated inefficiently. 具体来说, SUM()很可能是错误的,而MAX()的计算效率很低。

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

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