QueryDSL GroupBy and Sum
I have these QueryDSL statements:
BooleanExpression alwaysTrueExpression = match(null, null, datePeriod, period);
if (minAmount != null) {
alwaysTrueExpression = alwaysTrueExpression.and(aggregatedOrder.totalUnitAmountWithoutTax.sum().goe(minAmount));
}
if (maxAmount != null) {
alwaysTrueExpression = alwaysTrueExpression.and(aggregatedOrder.totalUnitAmountWithoutTax.sum().loe(maxAmount));
}
JPAQuery<AggregatedOrder> query = selectFrom(aggregatedOrder);
query = query.where(alwaysTrueExpression);
log.debug("sql={}", query);
return query.transform(GroupBy.groupBy(aggregatedOrder.tradingPromoterBranch.promoter).as(GroupBy.sum(aggregatedOrder.totalUnitAmountWithoutTax)));
But it throws the error:
Caused by: org.postgresql.util.PSQLException: ERROR: aggregate functions are not allowed in WHERE
I want to groupBy the aggregatedOrder by promoter and filter with amounts.
postgresql querydsl
add a comment |
I have these QueryDSL statements:
BooleanExpression alwaysTrueExpression = match(null, null, datePeriod, period);
if (minAmount != null) {
alwaysTrueExpression = alwaysTrueExpression.and(aggregatedOrder.totalUnitAmountWithoutTax.sum().goe(minAmount));
}
if (maxAmount != null) {
alwaysTrueExpression = alwaysTrueExpression.and(aggregatedOrder.totalUnitAmountWithoutTax.sum().loe(maxAmount));
}
JPAQuery<AggregatedOrder> query = selectFrom(aggregatedOrder);
query = query.where(alwaysTrueExpression);
log.debug("sql={}", query);
return query.transform(GroupBy.groupBy(aggregatedOrder.tradingPromoterBranch.promoter).as(GroupBy.sum(aggregatedOrder.totalUnitAmountWithoutTax)));
But it throws the error:
Caused by: org.postgresql.util.PSQLException: ERROR: aggregate functions are not allowed in WHERE
I want to groupBy the aggregatedOrder by promoter and filter with amounts.
postgresql querydsl
add a comment |
I have these QueryDSL statements:
BooleanExpression alwaysTrueExpression = match(null, null, datePeriod, period);
if (minAmount != null) {
alwaysTrueExpression = alwaysTrueExpression.and(aggregatedOrder.totalUnitAmountWithoutTax.sum().goe(minAmount));
}
if (maxAmount != null) {
alwaysTrueExpression = alwaysTrueExpression.and(aggregatedOrder.totalUnitAmountWithoutTax.sum().loe(maxAmount));
}
JPAQuery<AggregatedOrder> query = selectFrom(aggregatedOrder);
query = query.where(alwaysTrueExpression);
log.debug("sql={}", query);
return query.transform(GroupBy.groupBy(aggregatedOrder.tradingPromoterBranch.promoter).as(GroupBy.sum(aggregatedOrder.totalUnitAmountWithoutTax)));
But it throws the error:
Caused by: org.postgresql.util.PSQLException: ERROR: aggregate functions are not allowed in WHERE
I want to groupBy the aggregatedOrder by promoter and filter with amounts.
postgresql querydsl
I have these QueryDSL statements:
BooleanExpression alwaysTrueExpression = match(null, null, datePeriod, period);
if (minAmount != null) {
alwaysTrueExpression = alwaysTrueExpression.and(aggregatedOrder.totalUnitAmountWithoutTax.sum().goe(minAmount));
}
if (maxAmount != null) {
alwaysTrueExpression = alwaysTrueExpression.and(aggregatedOrder.totalUnitAmountWithoutTax.sum().loe(maxAmount));
}
JPAQuery<AggregatedOrder> query = selectFrom(aggregatedOrder);
query = query.where(alwaysTrueExpression);
log.debug("sql={}", query);
return query.transform(GroupBy.groupBy(aggregatedOrder.tradingPromoterBranch.promoter).as(GroupBy.sum(aggregatedOrder.totalUnitAmountWithoutTax)));
But it throws the error:
Caused by: org.postgresql.util.PSQLException: ERROR: aggregate functions are not allowed in WHERE
I want to groupBy the aggregatedOrder by promoter and filter with amounts.
postgresql querydsl
postgresql querydsl
edited Nov 23 '18 at 9:50
jarlh
29.8k52138
29.8k52138
asked Nov 23 '18 at 9:50
czetsuyaczetsuya
2,191124082
2,191124082
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
I think you are mixing two types of grouping supported by Querydsl:
- query.transform(...): groups result in-memory once data is fetched from database. In this case query doesn't cointain "group by" clause and transformation of data is executed on "java side".
- query.groupBy(...): add group by clause to your query. In this case grouping is executed by database.
If you need aggregation functions in your query, then you need to provide "group by" columns using "query.groupBy(...)".
add a comment |
Here's how I aggregate the column and filter by sum:
BooleanExpression alwaysTrueExpression = Expressions.asBoolean(true).isTrue();
NumberExpression<BigDecimal> totalUnitAmountWithoutTax = aggregatedOrder.totalUnitAmountWithoutTax.sum();
NumberExpression<Integer> totalQuantity = aggregatedOrder.quantity.sum();
NumberPath<Long> qPromoter = aggregatedOrder.tradingPromoterBranch.promoter.id;
BooleanExpression amountExpression = Expressions.asBoolean(true).isTrue();
if (minAmount != null) {
amountExpression = amountExpression.and(totalUnitAmountWithoutTax.goe(minAmount));
}
if (maxAmount != null) {
amountExpression = amountExpression.and(totalUnitAmountWithoutTax.loe(maxAmount));
}
JPAQuery<Tuple> query = jpaQueryFactory.select(qPromoter, totalUnitAmountWithoutTax, totalQuantity).from(aggregatedOrder).groupBy(qPromoter);
query = query.where(alwaysTrueExpression).having(amountExpression);
return query.fetch();
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444233%2fquerydsl-groupby-and-sum%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think you are mixing two types of grouping supported by Querydsl:
- query.transform(...): groups result in-memory once data is fetched from database. In this case query doesn't cointain "group by" clause and transformation of data is executed on "java side".
- query.groupBy(...): add group by clause to your query. In this case grouping is executed by database.
If you need aggregation functions in your query, then you need to provide "group by" columns using "query.groupBy(...)".
add a comment |
I think you are mixing two types of grouping supported by Querydsl:
- query.transform(...): groups result in-memory once data is fetched from database. In this case query doesn't cointain "group by" clause and transformation of data is executed on "java side".
- query.groupBy(...): add group by clause to your query. In this case grouping is executed by database.
If you need aggregation functions in your query, then you need to provide "group by" columns using "query.groupBy(...)".
add a comment |
I think you are mixing two types of grouping supported by Querydsl:
- query.transform(...): groups result in-memory once data is fetched from database. In this case query doesn't cointain "group by" clause and transformation of data is executed on "java side".
- query.groupBy(...): add group by clause to your query. In this case grouping is executed by database.
If you need aggregation functions in your query, then you need to provide "group by" columns using "query.groupBy(...)".
I think you are mixing two types of grouping supported by Querydsl:
- query.transform(...): groups result in-memory once data is fetched from database. In this case query doesn't cointain "group by" clause and transformation of data is executed on "java side".
- query.groupBy(...): add group by clause to your query. In this case grouping is executed by database.
If you need aggregation functions in your query, then you need to provide "group by" columns using "query.groupBy(...)".
answered Nov 29 '18 at 10:43
Piotr PonikowskiPiotr Ponikowski
5115
5115
add a comment |
add a comment |
Here's how I aggregate the column and filter by sum:
BooleanExpression alwaysTrueExpression = Expressions.asBoolean(true).isTrue();
NumberExpression<BigDecimal> totalUnitAmountWithoutTax = aggregatedOrder.totalUnitAmountWithoutTax.sum();
NumberExpression<Integer> totalQuantity = aggregatedOrder.quantity.sum();
NumberPath<Long> qPromoter = aggregatedOrder.tradingPromoterBranch.promoter.id;
BooleanExpression amountExpression = Expressions.asBoolean(true).isTrue();
if (minAmount != null) {
amountExpression = amountExpression.and(totalUnitAmountWithoutTax.goe(minAmount));
}
if (maxAmount != null) {
amountExpression = amountExpression.and(totalUnitAmountWithoutTax.loe(maxAmount));
}
JPAQuery<Tuple> query = jpaQueryFactory.select(qPromoter, totalUnitAmountWithoutTax, totalQuantity).from(aggregatedOrder).groupBy(qPromoter);
query = query.where(alwaysTrueExpression).having(amountExpression);
return query.fetch();
add a comment |
Here's how I aggregate the column and filter by sum:
BooleanExpression alwaysTrueExpression = Expressions.asBoolean(true).isTrue();
NumberExpression<BigDecimal> totalUnitAmountWithoutTax = aggregatedOrder.totalUnitAmountWithoutTax.sum();
NumberExpression<Integer> totalQuantity = aggregatedOrder.quantity.sum();
NumberPath<Long> qPromoter = aggregatedOrder.tradingPromoterBranch.promoter.id;
BooleanExpression amountExpression = Expressions.asBoolean(true).isTrue();
if (minAmount != null) {
amountExpression = amountExpression.and(totalUnitAmountWithoutTax.goe(minAmount));
}
if (maxAmount != null) {
amountExpression = amountExpression.and(totalUnitAmountWithoutTax.loe(maxAmount));
}
JPAQuery<Tuple> query = jpaQueryFactory.select(qPromoter, totalUnitAmountWithoutTax, totalQuantity).from(aggregatedOrder).groupBy(qPromoter);
query = query.where(alwaysTrueExpression).having(amountExpression);
return query.fetch();
add a comment |
Here's how I aggregate the column and filter by sum:
BooleanExpression alwaysTrueExpression = Expressions.asBoolean(true).isTrue();
NumberExpression<BigDecimal> totalUnitAmountWithoutTax = aggregatedOrder.totalUnitAmountWithoutTax.sum();
NumberExpression<Integer> totalQuantity = aggregatedOrder.quantity.sum();
NumberPath<Long> qPromoter = aggregatedOrder.tradingPromoterBranch.promoter.id;
BooleanExpression amountExpression = Expressions.asBoolean(true).isTrue();
if (minAmount != null) {
amountExpression = amountExpression.and(totalUnitAmountWithoutTax.goe(minAmount));
}
if (maxAmount != null) {
amountExpression = amountExpression.and(totalUnitAmountWithoutTax.loe(maxAmount));
}
JPAQuery<Tuple> query = jpaQueryFactory.select(qPromoter, totalUnitAmountWithoutTax, totalQuantity).from(aggregatedOrder).groupBy(qPromoter);
query = query.where(alwaysTrueExpression).having(amountExpression);
return query.fetch();
Here's how I aggregate the column and filter by sum:
BooleanExpression alwaysTrueExpression = Expressions.asBoolean(true).isTrue();
NumberExpression<BigDecimal> totalUnitAmountWithoutTax = aggregatedOrder.totalUnitAmountWithoutTax.sum();
NumberExpression<Integer> totalQuantity = aggregatedOrder.quantity.sum();
NumberPath<Long> qPromoter = aggregatedOrder.tradingPromoterBranch.promoter.id;
BooleanExpression amountExpression = Expressions.asBoolean(true).isTrue();
if (minAmount != null) {
amountExpression = amountExpression.and(totalUnitAmountWithoutTax.goe(minAmount));
}
if (maxAmount != null) {
amountExpression = amountExpression.and(totalUnitAmountWithoutTax.loe(maxAmount));
}
JPAQuery<Tuple> query = jpaQueryFactory.select(qPromoter, totalUnitAmountWithoutTax, totalQuantity).from(aggregatedOrder).groupBy(qPromoter);
query = query.where(alwaysTrueExpression).having(amountExpression);
return query.fetch();
answered Dec 3 '18 at 6:50
czetsuyaczetsuya
2,191124082
2,191124082
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444233%2fquerydsl-groupby-and-sum%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown