-->
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.  [ 5 posts ] 
Author Message
 Post subject: Using formula with SUM(column) with possibly no records
PostPosted: Tue Aug 16, 2005 12:24 pm 
Beginner
Beginner

Joined: Fri Jul 29, 2005 2:11 pm
Posts: 21
Hello. I have the following mapping but I'm getting an error possibly due to the fact that "amountAllocated" is null as a result of no rows.

Code:
<class name="AccountAllocation" table="ACCOUNT_ALLOCATION">>
   <id name="accountAllocationId" column="ACCT_ALCN_ID">
      <generator class="assigned"/>
   </id>      
   <property name="accountId" column="ACCT_ID" />
   <property name="amountAllocated"
           formula="(SELECT SUM(a.ALCN_AMT) FROM ALLOCATION a WHERE a.ACCT_ALCN_ID = ACCT_ALCN_ID)"/>

How do I get around this? There is a possibility that there might not be any "amountAllocated" (rows) in which case I'd like this set to 0 (zero). Also, is it possible that my database (Oracle 9i) might not support this?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 12:37 pm 
Regular
Regular

Joined: Thu May 26, 2005 2:08 pm
Posts: 99
Can you just add "AND a.ALCN_AMT IS NOT NULL" to your WHERE clause?

Or try this sum instead:

SUM(IF(a.ALCN_AMT IS NULL, 0, a.ALCN_AMT))

Note: That's what it would look like for MySQL. I don't know if those are appropriate for Oracle, but I'd have to believe that they have an equivilent set of methods.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 12:39 pm 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
In Oracle you would use the following in order to replace a NULL value with a certain other value:
Code:
NVL(column, 0)


Since NVL is Oracle-specific you can try the ANSI-SQL expression COALESCE in Hibernate:
Code:
SELECT SUM(COALESCE(a.ALCN_AMT, 0)) FROM ALLOCATION a WHERE a.ACCT_ALCN_ID = ACCT_ALCN_ID


See chapter 15.8 of the documentation.

Best regards
Sven

_________________
Please don't forget to give credit, if this posting helped to solve your problem.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 12:41 pm 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
Well, you can also use NVL - it's native SQL... :)

Best regards
Sven

_________________
Please don't forget to give credit, if this posting helped to solve your problem.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 12:57 pm 
Beginner
Beginner

Joined: Fri Jul 29, 2005 2:11 pm
Posts: 21
Both of these worked! Thanks a bunch.
Code:
SELECT COALESCE(SUM(a.ALCN_AMT), 0)
SELECT NVL(SUM(a.ALCN_AMT), 0)


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

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.