我可以将此SQL代码重写为querydsl吗?

问题描述

我有sql代码,用于计算不同谓词在db中的记录数

    SELECT
    count(*) as total_count,count(notAided) as not_aided_count,count(chatAided) as chat_aided_count
FROM (
    SELECT
        CASE WHEN aided_with IS NULL THEN 1 END notAided,CASE WHEN aided_with = 'CHAT' THEN 1 END chatAided
    FROM sessions
) sessions

我可以用querydsl重写它吗?我看了一下CaseBuilder,但不知道如何在选择查询中使用它

解决方法

一个人可以写:

query.select(
     QSession.session.id.count().as("total_count"),new CaseBuilder().when(QSession.session.aidedWith.isNotNull()).then(1).otherwise(Expressions.<Integer> nullExpression()).count().as("not_aided_count"),new CaseBuilder().when(QSession.session.aidedWith.eq("CHAT")).then(1).otherwise(Expressions.<Integer> nullExpression()).count().as("chat_aided_count")
)

与SQL等效:

SELECT
    count(*) as total_count,count(CASE WHEN aided_with IS NULL THEN 1 END) as not_aided_count,count(CASE WHEN aided_with = 'CHAT' THEN 1 END) as chat_aided_count
FROM sessions

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...