将复杂的嵌套 SQL 子查询转换为 JOOQ
Translate complex nested SQL subqueries to JOOQ
我在将 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;
return
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);
return
我得到的错误:'field list' 中的未知列 'sugActive'。
问题是这样的:
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));
我在将 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;
return
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);
return
我得到的错误:'field list' 中的未知列 'sugActive'。
问题是这样的:
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));