我正在尝试使用 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 guiasfrom (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 ) solicitudesgroup by emisor, anio, mesorder 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/ )的文档并尝试寻找类似的其他地方的案例,但不幸的是还没有成功。
1 回答
万千封印
TA贡献1891条经验 获得超3个赞
这里的问题是您将某些变量声明为类型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(初始化程序中的部分。我认为这与您所做的测试有关。)
我认为此更改应该可以解决您的问题。
添加回答
举报
0/150
提交
取消