jOOQ 3.5.3 中的案例表达式和别名
Case expressions and aliases in jOOQ 3.5.3
我在 jOOQ 中表达以下查询时遇到问题
SELECT s.season_name, l.league_name,
COUNT(CASE WHEN m.full_time_result = 'H' THEN 'H' END) AS home_wins,
COUNT(CASE WHEN m.full_time_result = 'D' THEN 'D' END) AS draws,
COUNT(CASE WHEN m.full_time_result = 'A' THEN 'A' END) AS away_wins,
COUNT(m.full_time_result) AS all_matches
FROM football_stats.matches AS m
JOIN football_stats.seasons AS s USING (season_id)
JOIN football_stats.leagues AS l USING (league_id) GROUP BY s.season_name, l.league_name
我的 java 代码是:
final Field<String> homeWins = DSL.decode().when(MATCHES.FULL_TIME_RESULT.eq(HOME_WIN), HOME_WIN).as("home_wins");
final Field<String> draws = DSL.decode().when(MATCHES.FULL_TIME_RESULT.eq(DRAW), DRAW).as("draws");
final Field<String> awayWins = DSL.decode().when(MATCHES.FULL_TIME_RESULT.eq(AWAY_WIN), AWAY_WIN).as("away_wins");
final Field<?>[] fields = {SEASONS.SEASON_NAME, LEAGUES.LEAGUE_NAME, DSL.count(homeWins), DSL.count(draws), DSL.count(awayWins), DSL.count()};
final SelectQuery<Record> query = dslContext.selectQuery();
query.addSelect(fields);
query.addFrom(MATCHES);
query.addJoinOnKey(SEASONS, JoinType.JOIN, Keys.MATCHES__FK_MATCHES_SEASONS);
query.addJoinOnKey(LEAGUES, JoinType.JOIN, Keys.MATCHES__FK_MATCHES_LEAGUES);
query.addGroupBy(SEASONS.SEASON_NAME,LEAGUES.LEAGUE_NAME);
final Record result = query.fetchOne();
而 jOOQ 生成的查询是:
select
"football_stats"."seasons"."season_name",
"football_stats"."leagues"."league_name",
count("home_wins"),
count("draws"),
count("away_wins"),
count(*)
from "football_stats"."matches"
join "football_stats"."seasons"
on "football_stats"."matches"."season_id" = "football_stats"."seasons"."season_id"
join "football_stats"."leagues"
on "football_stats"."matches"."league_id" = "football_stats"."leagues"."league_id"
group by
"football_stats"."seasons"."season_name",
"football_stats"."leagues"."league_name"
结果是
ERROR: column "home_wins" does not exist
但是,当我省略.as("alias_name")
时,jOOQ会生成
select
"football_stats"."seasons"."season_name",
"football_stats"."leagues"."league_name",
count(case when "football_stats"."matches"."full_time_result" = 'H' then 'H' end),
count(case when "football_stats"."matches"."full_time_result" = 'D' then 'D' end),
count(case when "football_stats"."matches"."full_time_result" = 'A' then 'A' end),
count(*)
from "football_stats"."matches"
join "football_stats"."seasons"
on "football_stats"."matches"."season_id" = "football_stats"."seasons"."season_id"
join "football_stats"."leagues"
on "football_stats"."matches"."league_id" = "football_stats"."leagues"."league_id"
group by
"football_stats"."seasons"."season_name",
"football_stats"."leagues"."league_name"
如何让 jOOQ 像 count(case when "football_stats"."matches"."full_time_result" = 'A' then 'A' end) as 'away_wins'
一样生成它?
此外,我可以让它生成 USING
而不是 ON
(addJoinUsing
方法也会生成 ON
)吗?
您为错误的表达式添加了别名
在SQL中,您写道:
COUNT(CASE WHEN m.full_time_result = 'H' THEN 'H' END) AS home_wins
使用 jOOQ,您写道:
homeWins = decode().when(MATCHES.FULL_TIME_RESULT.eq(HOME_WIN), HOME_WIN).as("home_wins");
然后:
DSL.count(homeWins);
所以,jOOQ 生成了你写的 SQL。
然而,你打算写这个:
homeWins = decode().when(MATCHES.FULL_TIME_RESULT.eq(HOME_WIN), HOME_WIN);
然后:
DSL.count(homeWins).as("home_wins");
旁注:
在即将推出的 jOOQ 3.6 中,您将能够通过使用 SQL standard FILTER
clause on aggregate functions:
来简化您的 COUNT(CASE...)
表达式
DSL.count().filterWhere(MATCHES.FULL_TIME_RESULT.eq(HOME_WIN)).as("home_wins")
我在 jOOQ 中表达以下查询时遇到问题
SELECT s.season_name, l.league_name,
COUNT(CASE WHEN m.full_time_result = 'H' THEN 'H' END) AS home_wins,
COUNT(CASE WHEN m.full_time_result = 'D' THEN 'D' END) AS draws,
COUNT(CASE WHEN m.full_time_result = 'A' THEN 'A' END) AS away_wins,
COUNT(m.full_time_result) AS all_matches
FROM football_stats.matches AS m
JOIN football_stats.seasons AS s USING (season_id)
JOIN football_stats.leagues AS l USING (league_id) GROUP BY s.season_name, l.league_name
我的 java 代码是:
final Field<String> homeWins = DSL.decode().when(MATCHES.FULL_TIME_RESULT.eq(HOME_WIN), HOME_WIN).as("home_wins");
final Field<String> draws = DSL.decode().when(MATCHES.FULL_TIME_RESULT.eq(DRAW), DRAW).as("draws");
final Field<String> awayWins = DSL.decode().when(MATCHES.FULL_TIME_RESULT.eq(AWAY_WIN), AWAY_WIN).as("away_wins");
final Field<?>[] fields = {SEASONS.SEASON_NAME, LEAGUES.LEAGUE_NAME, DSL.count(homeWins), DSL.count(draws), DSL.count(awayWins), DSL.count()};
final SelectQuery<Record> query = dslContext.selectQuery();
query.addSelect(fields);
query.addFrom(MATCHES);
query.addJoinOnKey(SEASONS, JoinType.JOIN, Keys.MATCHES__FK_MATCHES_SEASONS);
query.addJoinOnKey(LEAGUES, JoinType.JOIN, Keys.MATCHES__FK_MATCHES_LEAGUES);
query.addGroupBy(SEASONS.SEASON_NAME,LEAGUES.LEAGUE_NAME);
final Record result = query.fetchOne();
而 jOOQ 生成的查询是:
select
"football_stats"."seasons"."season_name",
"football_stats"."leagues"."league_name",
count("home_wins"),
count("draws"),
count("away_wins"),
count(*)
from "football_stats"."matches"
join "football_stats"."seasons"
on "football_stats"."matches"."season_id" = "football_stats"."seasons"."season_id"
join "football_stats"."leagues"
on "football_stats"."matches"."league_id" = "football_stats"."leagues"."league_id"
group by
"football_stats"."seasons"."season_name",
"football_stats"."leagues"."league_name"
结果是
ERROR: column "home_wins" does not exist
但是,当我省略.as("alias_name")
时,jOOQ会生成
select
"football_stats"."seasons"."season_name",
"football_stats"."leagues"."league_name",
count(case when "football_stats"."matches"."full_time_result" = 'H' then 'H' end),
count(case when "football_stats"."matches"."full_time_result" = 'D' then 'D' end),
count(case when "football_stats"."matches"."full_time_result" = 'A' then 'A' end),
count(*)
from "football_stats"."matches"
join "football_stats"."seasons"
on "football_stats"."matches"."season_id" = "football_stats"."seasons"."season_id"
join "football_stats"."leagues"
on "football_stats"."matches"."league_id" = "football_stats"."leagues"."league_id"
group by
"football_stats"."seasons"."season_name",
"football_stats"."leagues"."league_name"
如何让 jOOQ 像 count(case when "football_stats"."matches"."full_time_result" = 'A' then 'A' end) as 'away_wins'
一样生成它?
此外,我可以让它生成 USING
而不是 ON
(addJoinUsing
方法也会生成 ON
)吗?
您为错误的表达式添加了别名
在SQL中,您写道:
COUNT(CASE WHEN m.full_time_result = 'H' THEN 'H' END) AS home_wins
使用 jOOQ,您写道:
homeWins = decode().when(MATCHES.FULL_TIME_RESULT.eq(HOME_WIN), HOME_WIN).as("home_wins");
然后:
DSL.count(homeWins);
所以,jOOQ 生成了你写的 SQL。
然而,你打算写这个:
homeWins = decode().when(MATCHES.FULL_TIME_RESULT.eq(HOME_WIN), HOME_WIN);
然后:
DSL.count(homeWins).as("home_wins");
旁注:
在即将推出的 jOOQ 3.6 中,您将能够通过使用 SQL standard FILTER
clause on aggregate functions:
COUNT(CASE...)
表达式
DSL.count().filterWhere(MATCHES.FULL_TIME_RESULT.eq(HOME_WIN)).as("home_wins")