QueryDSL GroupBy and Sum












0















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.










share|improve this question





























    0















    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.










    share|improve this question



























      0












      0








      0








      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.










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 9:50









      jarlh

      29.8k52138




      29.8k52138










      asked Nov 23 '18 at 9:50









      czetsuyaczetsuya

      2,191124082




      2,191124082
























          2 Answers
          2






          active

          oldest

          votes


















          1














          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(...)".






          share|improve this answer































            1














            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();





            share|improve this answer
























              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
              });


              }
              });














              draft saved

              draft discarded


















              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









              1














              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(...)".






              share|improve this answer




























                1














                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(...)".






                share|improve this answer


























                  1












                  1








                  1







                  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(...)".






                  share|improve this answer













                  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(...)".







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 29 '18 at 10:43









                  Piotr PonikowskiPiotr Ponikowski

                  5115




                  5115

























                      1














                      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();





                      share|improve this answer




























                        1














                        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();





                        share|improve this answer


























                          1












                          1








                          1







                          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();





                          share|improve this answer













                          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();






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Dec 3 '18 at 6:50









                          czetsuyaczetsuya

                          2,191124082




                          2,191124082






























                              draft saved

                              draft discarded




















































                              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.




                              draft saved


                              draft discarded














                              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





















































                              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







                              這個網誌中的熱門文章

                              Academy of Television Arts & Sciences

                              Literary criticism

                              The Gentleman's Magazine