【问题标题】:Translate complex nested SQL subqueries to JOOQ将复杂的嵌套 SQL 子查询转换为 JOOQ
【发布时间】:2020-08-27 13:22:48
【问题描述】:

我在将 SQL 查询转换为 JOOQ 时遇到了一些问题。所以 join 子句中的别名 sugActive 是未知的。可能有更好的方法来写这个。所以,我仍然无法弄清楚出了什么问题。我感谢任何提示,在此先感谢。

1. The SQL query:

        select
        count(campId) as 'campCount',
        count(case when targetTest != 0 then campId else null end) as 'targetTestSet',
        count(case when targetTest = 0 then campId else null end) as 'targetTestNotSet',
        coalesce(sum(sugActive), 0) as 'sugActive',
        coalesce(sum(sugApproved), 0) as 'sugApproved',
    from (
        select 
            c.venId,
            c.name,
            s.*
        from campTable c
        join (select s.campId,
                    count(case when s.status = 'ACTIVE'
                        then id end) as 'sugActive',
                    count(case when s.status = 'APPROVED'
                        then id end) as 'sugApproved',
                from sugTable s
                group by s.campId
            ) s on c.campId = s.campId
        where c.state = 'enabled'
    ) a;

返回

2. The JOOQ query:

    Field<Integer> sugActive = igDb
    .select(
        count(when(
            sugTable.status.eq("ACTIVE"), sugTable.campId)
            .as("sugActive")))
    .from(sugTable).asField("sugActive");
Field<Integer> sugApproved = igDb
    .select(
            count(when(
                sugTable.status.eq("APPROVED"), sugTable.campId)
                .as("sugApproved")))
    .from(sugTable).asField("sugApproved");
IgOrgSumRecord result = igDb
    .select(
        count(campTable.venId).as("venIdCount"),
        count(sugTable.campId).as("campCount"),
        count(when(
            campTable.targetTest.notEqual(BigDecimal.ZERO), sugTable.campId
            ).otherwise("")).as("targetTestSet"),
        count(when(
            campTable.targetTest.eq(BigDecimal.ZERO), sugTable.campId
            ).otherwise("")).as("targetTestNotSet"),
        coalesce(sum(sugActive)).as("sugActive"),
        coalesce(sum(sugApproved)).as("sugApproved"))
    .from(select(
        campTable.venId,
        campTable.name,
        sugTable.asterisk()
    )
        .from(campTable.as("c"))
        .join(
            select(
                sugTable.campId,
                count(when(
                    sugTable.status.eq("ACTIVE"), sugTable.campId)
                    .as("sugActive")),
                count(when(
                    sugTable.status.eq("APPROVED"), sugTable.campId)
                    .as("sugApproved"))
            )
                .from(sugTable)
                .groupBy(sugTable.campId)
        )
        .on(campTable.campId.eq(sugTable.campId))
        .where(campTable.state.eq("enabled"))
    )
.fetchOneInto(IgOrgSumRecord.class);

返回

我得到的错误:“字段列表”中的未知列“sugActive”。

【问题讨论】:

    标签: java mysql sql spring-boot jooq


    【解决方案1】:

    问题是这样的:

    Field<Integer> sugActive = igDb
        .select(
            count(when(
                sugTable.status.eq("ACTIVE"), sugTable.campId)
                .as("sugActive")))
        .from(sugTable).asField("sugActive");
    

    这不仅仅是一个列表达式,它是一个别名声明(或引用)。然后,当您真正想要使用表达式时,您将使用别名引用,重新为其设置别名:

    coalesce(sum(sugActive)).as("sugActive"),
    

    相反,像这样声明您的 sugActive 变量:

    Field<Integer> sugActive = field(igDb
        .select(
            count(when(
                sugTable.status.eq("ACTIVE"), sugTable.campId)
                .as("sugActive")))
        .from(sugTable));
    

    另请参阅此处的说明:https://github.com/jOOQ/jOOQ/issues/10119

    【讨论】:

      猜你喜欢
      • 2018-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多