Java 将带有case子句的Sql转换为Hql
我需要将此sql查询转换为hql Sql查询:Java 将带有case子句的Sql转换为Hql,java,mysql,sql,hibernate,Java,Mysql,Sql,Hibernate,我需要将此sql查询转换为hql Sql查询: SELECT COALESCE(SUM(t2.Ammount * CASE WHEN t2.idAccountDeb = c.id AND t2.idAccountDeb<>t2.idAccountCred THEN 1 WHEN t2.idAccountCred AND t2.idAccountDeb<>t2.idAccountCred THEN (-1) ELSE (0) END),0)
SELECT COALESCE(SUM(t2.Ammount * CASE WHEN t2.idAccountDeb = c.id AND t2.idAccountDeb<>t2.idAccountCred THEN 1
WHEN t2.idAccountCred AND t2.idAccountDeb<>t2.idAccountCred THEN (-1) ELSE (0) END),0)
选择合并(SUM(t2.amount*当t2.idAccountDeb=c.id和t2.idaccountdeb2.idAccountCred时为1
当t2.idAccountCred和t2.idaccountdebut2.idAccountCred时,-1)否则(0)结束,0)
不起作用的Hql查询:
(select COALESCE(SUM(t2.Ammount * CASE WHEN t2.AccountDeb = c AND t2.AccountDeb<>t2.AccountCred THEN 1
WHEN t2.AccountCred AND t2.AccountDeb<>t2.AccountCred THEN (-1) ELSE (0) END),0)
(当t2.AccountDeb=c和t2.accountdeb2.AccountCred分别为1时,选择COALESCE(SUM)(t2.amount*CASE)
当t2.AccountCred和t2.accountdebut2.AccountCred之后(-1)否则(0)结束时,0)
AST休眠错误:
ERROR: <AST>: 2: 24: unexpected AST node:.
错误::2:24:意外的AST节点:。
这是MySQL:
SELECT COALESCE(SUM(CASE WHEN t2.idAccountDeb t2.valor * = c.id AND t2.idAccountDeb <> 1
THEN t2.idAccountCred
WHEN AND t2.idAccountCred t2.idAccountDeb <> t2.idAccountCred
THEN (-1)
ELSE (0)
END), 0)
这种逻辑应该在MySQL和HQL中都能工作
编辑:
这两个数据库中都应该可以使用:
SELECT SUM(t2.Ammount * (CASE WHEN t2.idAccountDeb = c.id AND t2.idAccountDeb = t2.idAccountCred
THEN 1
WHEN t2.idAccountDeb = c.id AND t2.idAccountDeb <> t2.idAccountCred
THEN -1
ELSE 0
END)
)
选择SUM(t2.amount*(t2.idAccountDeb=c.id和t2.idAccountDeb=t2.idAccountCred时的情况
那么1
当t2.idAccountDeb=c.id和t2.idAccountDeb t2.idAccountCred
然后-1
其他0
(完)
)
感谢您提供关于当前开发工作状态的报告。您有什么问题要问吗?(顺便说一句,示例SQL查询中存在语法问题;SQL查询不可能不产生错误。)抱歉,键入错误。我将重做。我的英语很差。除了那些语法问题,SQL查询似乎仍然会产生错误,而不引用t2
rowsource.remade。抱歉,键入错误。我将重做。我的英语很差,非常好。我两小时前在巴西做了同样的问题,什么都没有。时间是周一哎呀,我做的代码中进行了一些修改,但解决了case子句的问题。SELECT SUM(t2.amount*(当t2.idAccountDeb=c.id和t2.idAccountDeb t2.idAccountCred时为1,当t2.idAccountCred=c.id和t2.idAccountDeb t2.idAccountCred时为1,否则为0结束))
SELECT SUM(t2.Ammount * (CASE WHEN t2.idAccountDeb = c.id AND t2.idAccountDeb = t2.idAccountCred
THEN 1
WHEN t2.idAccountDeb = c.id AND t2.idAccountDeb <> t2.idAccountCred
THEN -1
ELSE 0
END)
)