Latest year for cost kind
#CTCTOWALTZ-2075
@@ -84,8 +84,10 @@ public class CostDao {
public Set<EntityCost> findBySelectorForYear(GenericSelector genericSelector){
-
- SelectHavingStep<Record2<Long, Integer>> cost_kind_latest_year = getLatestYearSubSelect();
+ SelectHavingStep<Record2<Long, Integer>> cost_kind_latest_year = DSL
+ .select(COST.COST_KIND_ID, DSL.max(COST.YEAR).as("latest_year"))
+ .from(COST)
+ .groupBy(COST.COST_KIND_ID);
Condition latest_year_for_kind = COST.COST_KIND_ID.eq(cost_kind_latest_year.field(COST.COST_KIND_ID))
.and(COST.YEAR.eq(cost_kind_latest_year.field("latest_year", Integer.class)));
@@ -105,30 +107,22 @@ public class CostDao {
GenericSelector genericSelector,
int limit){
- SelectHavingStep<Record2<Long, Integer>> cost_kind_latest_year = getLatestYearSubSelect();
-
- Condition latest_year_for_kind = COST.COST_KIND_ID.eq(cost_kind_latest_year.field(COST.COST_KIND_ID))
- .and(COST.YEAR.eq(cost_kind_latest_year.field("latest_year", Integer.class)));
+ SelectConditionStep<Record1<Integer>> latestYear = DSL
+ .select(DSL.max(COST.YEAR).as("latest_year"))
+ .from(COST)
+ .where(COST.COST_KIND_ID.eq(costKindId));
return dsl
.select(ENTITY_NAME_FIELD)
.select(COST.fields())
.from(COST)
- .innerJoin(cost_kind_latest_year).on(dsl.renderInlined(latest_year_for_kind))
.where(COST.ENTITY_ID.in(genericSelector.selector())
.and(COST.ENTITY_KIND.eq(genericSelector.kind().name())))
.and(COST.COST_KIND_ID.eq(costKindId))
+ .and(COST.YEAR.eq(latestYear))
.orderBy(COST.AMOUNT.desc())
.limit(limit)
.fetchSet(TO_COST_MAPPER);
}
-
- private SelectHavingStep<Record2<Long, Integer>> getLatestYearSubSelect() {
- return DSL
- .select(COST.COST_KIND_ID, DSL.max(COST.YEAR).as("latest_year"))
- .from(COST)
- .groupBy(COST.COST_KIND_ID);
- }
-
}
