jOOQ 3.5.3中的案例表达式和别名

时间:2015-04-18 11:42:34

标签: java jooq

我在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代替ONaddJoinUsing方法也生成ON)?

1 个答案:

答案 0 :(得分:2)

您将错误的表达式别名

在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")