我正在嘗試使用 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
提交
取消