Converting SQL with subselect in select to HQL

Posted by UltraVi01 on Stack Overflow See other posts from Stack Overflow or by UltraVi01
Published on 2010-04-30T18:26:47Z Indexed on 2010/04/30 18:27 UTC
Read the original article Hit count: 189

Filed under:
|
|
|

I have the following SQL that I am having problems converting to HQL. A NPE is getting thrown -- which I think has something to do with the SUM function. Also, I'd like to sort on the subselect alias -- is this possible?

SQL (subselect):

SELECT q.title, q.author_id, 
(SELECT IFNULL(SUM(IF(vote_up=true,1,-1)), 0) 
FROM vote WHERE question_id = q.id) AS votecount

FROM question q ORDER BY votecount DESC

HQL (not working)

SELECT q, 
(SELECT COALESCE(SUM(IF(v.voteUp=true,1,-1)), 0) 
FROM Vote v WHERE v.question = q) AS votecount
FROM Question AS q
LEFT JOIN q.author u
LEFT JOIN u.blockedUsers ub
WHERE q.dateCreated BETWEEN :week AND :now
AND u.id NOT IN (
    SELECT ub.blocked FROM UserBlock AS ub WHERE ub.blocker =:loggedInUser
)
AND (u.blockedUsers IS EMPTY OR ub.blocked !=:loggedInUser) 
ORDER BY votecount DESC

© Stack Overflow or respective owner

Related posts about hql

Related posts about sql