-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Using correctly SQL COALESCE
PostPosted: Tue Mar 06, 2012 11:00 am 
Newbie

Joined: Fri Apr 01, 2011 3:18 am
Posts: 1
Dear,
I've a big trouble.

If I write a SQL query as follow:
Code:
SELECT
   coalesce(
      (select sum(field1) from table t2 where t2.fk = t.id and other conditions)
   ,0)
FROM table t


In HQL I try to convert in follow query:
Code:
SELECT new MyObject(
   coalesce(
      (select sum(t2.field1) from table t2 where t2.fk = t.id and other conditions)
   ,0)
)
FROM table t


But the HQL parser goes in error because I can't use coalesce with a sub select include. The only way accepted by Hibernate is the follow:
Code:
(SELECT coalesce(SUM(t2.field1), 0) from table t2 where t2.fk = t.id other conditions)

But is not correct because, in SQL
if all conditions of subquery are satisfied and the sum is null coalesce returns 0, BUT if some conditions are not satisfied all the select is null and only an external coalesce respect the query can put 0 value.

Finally, MyObject constructor don't want NULL value as parameter, so I don't know how to resolve this brain teaser!!!
Help me please!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.