[英]To convert Sql with case clause to Hql
I need to convert this sql query to hql. 我需要将此SQL查询转换为hql。
Sql query: 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)
Hql query that did not work: 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)
AST Hibernate error: AST休眠错误:
ERROR: <AST>: 2: 24: unexpected AST node:.
This is the MySQL: 这是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)
This code is not syntactically correct. 该代码在语法上不正确。
*=
is not a MySQL operator (the list is here ). *=
不是MySQL运算符(列表在此处 )。 The second when
misformed. 格式错误
when
的第二个。 I might guess that the intended logic is: 我可能会猜到预期的逻辑是:
SELECT SUM(CASE WHEN t2.idAccountDeb = t2.valor AND t2.idAccountDeb <> 1
THEN t2.idAccountCred
WHEN t2.idAccountDeb <> t2.idAccountCred
THEN -1
ELSE 0
END)
Or something like this. 或类似的东西。 This logic should work in both MySQL and HQL.
该逻辑在MySQL和HQL中均应起作用。
EDIT: 编辑:
Something like this should work in both databases: 这样的事情在两个数据库中都应该起作用:
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)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.