不兼容的类型不允许在 JOOQ dsl 中合并子查询

我正在尝试使用 JOOQ 以编程方式在 Java 中构建以下查询:


 select emisor,

       anio,

       mes,

       sum(case when codigo = '01' then total else 0 end) as facturas,

       sum(case when codigo = '03' then total else 0 end) as boletas,

       sum(case when codigo = '07' then total else 0 end) as notas_credito,

       sum(case when codigo = '08' then total else 0 end) as notas_debito,

       sum(case when codigo = 'RC' then total else 0 end) as resumenes,

       sum(case when codigo = 'RA' then total else 0 end) as anulaciones,

       sum(case when codigo = '40' then total else 0 end) as percepciones,

       sum(case when codigo = '20' then total else 0 end) as retenciones,

       sum(case when codigo = 'RV' then total else 0 end) as reversiones,

       sum(case when codigo = '09' then total else 0 end) as guias

from (select ruc_emisor                      as emisor,

             year(fec_registro)              as anio,

             month(fec_registro)             as mes,

             substring(nom_solicitud, 13, 2) as codigo,

             count(*)                        as total

      from bd_ose.tx_solicitud

      where year(fec_registro) = '2019'

        and month(fec_registro) = 7

      group by ruc_emisor, anio, mes, codigo

      UNION

      select num_ruc             as emisor,

             year(fec_registro)  as anio,

             month(fec_registro) as mes,

             cod_cpe             as codigo,

             count(*)            as total

      from bd_ose.tx_comprobante_inf

      where year(fec_registro) = '2019'

        and month(fec_registro) = 7

      group by num_ruc, anio, mes, codigo

     ) solicitudes

group by emisor, anio, mes

order by emisor;


在 SQL 和 JOOQ 方面,我仍然相当缺乏经验,但我决定从内部开始,逐步解决问题。当我尝试将 .union() 方法应用于两个内部子查询时,我遇到了问题。我的 IDE 突出显示了一个类型不匹配错误,指出 union 需要一个类型为“org.jooq.Select<...”的参数,而我提供的参数类型为“org.jooq.SelectHavingStep<... " - 这是从 .groupBy() 返回的类型


我已经检查了 [union docs] ( https://www.jooq.org/doc/3.11/manual/sql-building/sql-statements/select-statement/union-clause/ )的文档并尝试寻找类似的其他地方的案例,但不幸的是还没有成功。


呼如林
浏览 111回答 1
1回答

万千封印

这里的问题是您将某些变量声明为类型Field<?>,结果 Java 编译器认为这两个Select对象不兼容。所以不是:Field<?> CIemisor = txComprobanteInf.NUM_RUC.as("emisor");Field<?> CIcodigo = txComprobanteInf.COD_CPE.as("codigo");您应该使用适当的通用类型参数声明这两个变量。例如Field<String> CIemisor = txComprobanteInf.NUM_RUC.as("emisor");Field<String> CIcodigo = txComprobanteInf.COD_CPE.as("codigo");其他两个变量也是如此。(我注意到对于变量,Semisor您需要删除dslContext.select(初始化程序中的部分。我认为这与您所做的测试有关。)我认为此更改应该可以解决您的问题。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java