方法orderby / group by /在查询jOOQ中找不到

时间:2017-12-10 18:49:22

标签: java sql jooq

我是jOOQ的新手,我必须将查询翻译成jOOQ;

我的表“CN_THEODOINO”包括两个字段:NAM(表示YEAR)和THANG(表示MONTH),数据类型为short和byte。

我想计算sysdate与该表中NAM和THANG之间的不同月份。

之后,我将与我的参数中的p_diff进行比较,并选择大于它的那些。

我试试这个:

public List<CN_DS_NO> layDsKHangNo3(String sInputParam) throws JSONException {
    try {
        Calendar c = Calendar.getInstance();
        int currMonths = c.get(Calendar.YEAR) * 12 + c.get(Calendar.MONTH);
        JSONObject jsonObj = new JSONObject(sInputParam);
        short p_diff    =    (short)jsonObj.getInt("SO_THANGNO");
        Condition condition = DSL.trueCondition();
        condition = condition.and(bangDsChuyenNo2.TIEN_NO.gt(Long.valueOf(0)));
        condition = condition.and(bangDsChuyenNo2.NAM.mul(12).add(bangDsChuyenNo2.THANG).add(p_diff).sub(currMonths).lessOrEqual((short)0));
        condition = condition.groupBy(bangDsChuyenNo2.MA_KHANG);
        condition = condition.having(DSL.count(bangDsChuyenNo2.ID_HDON).gt(jsonObj.getInt("SO_HOADON")).and(DSL.sum((Field) field(bangDsChuyenNo2.TIEN_NO)).gt(jsonObj.getLong("TONG_NO"))));

        qDsChuyenNo = this.getCreate()
                        .select()
                        .from(bangDsChuyenNo)
                        .where(condition);
        return nativeQuery(this.getEm(), qDsChuyenNo, CN_DS_NO.class);

            } catch (Exception ex) {
            throw ex;
            }

我得到两个错误,比如说:

错误(141,34):找不到符号; symbol:方法groupBy(org.jooq.TableField); location:类型为org.jooq.Condition的可变条件

错误(142,34):找不到符号;符号:方法有(org.jooq.Condition); location:类型为org.jooq.Condition的可变条件

那我怎么能在jOOQ中做到这一点。我已经在谷歌和我们的论坛搜索但无法得到答案。请帮帮我。

1 个答案:

答案 0 :(得分:0)

org.jooq.Condition类型为SQL谓词建模。它具有使用ANDOR运算符组合谓词的方法,但肯定没有属于SELECT查询的方法,例如GROUP BYHAVING。只需将这些条款移到他们所属的位置:

Condition condition = DSL.trueCondition();
condition = condition.and(bangDsChuyenNo2.TIEN_NO.gt(Long.valueOf(0)));
condition = condition.and(bangDsChuyenNo2.NAM.mul(12).add(bangDsChuyenNo2.THANG).add(p_diff).minus(currMonths).greaterThan((short)0));

// These calls make no sense at this position:
// condition = condition.groupBy(bangDsChuyenNo2.MA_KHANG);
// condition = condition.having(DSL.count(bangDsChuyenNo2.ID_HDON).gt(jsonObj.getInt("SO_HOADON")).and(DSL.sum((Field) field(bangDsChuyenNo2.TIEN_NO)).gt(jsonObj.getLong("TONG_NO"))));

qDsChuyenNo = this.getCreate()
                  .select()
                  .from(bangDsChuyenNo)
                  .where(condition)

                  // Above clauses moved down here:
                  .groupBy(bangDsChuyenNo2.MA_KHANG)
                  .having(DSL.count(bangDsChuyenNo2.ID_HDON)
                             .gt(jsonObj.getInt("SO_HOADON"))
                             .and(DSL.sum((Field) field(bangDsChuyenNo2.TIEN_NO))
                             .gt(jsonObj.getLong("TONG_NO"))))
                  ;