select count(*) / count(*) ???

162 views
Skip to first unread message

(메가존)곽지은

unread,
Aug 26, 2020, 5:11:40 PM8/26/20
to Querydsl

Hello. Please let me know if you have a good way.

---------------------------------------------------------------------------------------------------

NumberPath<Long> count1 = Expressions.numberPath(Long.class, “count1”);

NumberPath<Long> count2 = Expressions.numberPath(Long.class, “count2”);

NumberPath<Long> count3 = Expressions.numberPath(Long.class, “count3”);


JPQLQuery<OnlineTrainingCourseDto> query = getQuerydsl().createQuery()

    .select(

        new Dto(

            ExpressionUtils.as(

                JPAExpressions.select(table2.table2Seq.count())

                        .from(table2)

                        .where(table2.table2Seq.eq(table1.table1Seq)),

                count1

            ),

            ExpressionUtils.as(

                JPAExpressions.select(table2.table2Seq.count())

                        .from(table2)

                        .where(table2.table2Seq.eq(table1.table1Seq)

                        .and(table2.table2Yn.eq(true))),

                count2

            ),      

????? count1 / count2 as count3 ?????

            )

        )

    )

    .from(table1)


---------------------------------------------------------------------------------


I want the query below. What should I do? plz...

select 

          (select count(*) from table2 where table2.table2Seq = table1.table1Seq) as count1,

          (select count(*) from table2 where table2.table2Seq = table1.table1Seq and table2Yn = true) as count2,

          (select count(*) from table2 where table2.table2Seq = table1.table1Seq) / (select count(*) from table2 where table2.table2Seq = table1.table1Seq and table2Yn = true) as count3

from

          table1

order by 

         count3



Nathan Molinari

unread,
Sep 9, 2020, 6:03:36 AM9/9/20
to Querydsl
If is not a problem to you, i suggest to make the division of count1 and count2 by coding in java after fetch the data in database.

Probably the database will execute again the two count queries to do the division. Which is unnecessary.

Reply all
Reply to author
Forward
0 new messages