If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > How to replace 0

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-21-10, 12:02
heyit heyit is offline
Registered User
 
Join Date: May 2010
Posts: 12
How to replace 0

I'm 100% new to DB2. We have a bug in code where one returning value making total result incorrectly '0' (zero).

Here is the statement which we are executing:

SELECT SUM(MULTIPLY_ALT(A_QTY, B_QTY, C_QTY)) from CUST_QUANTITY;

I believe this statement is multiplying A_QTY, B_QTY, C_QTY for each row and later adding all the rows (please correct me if I'm not).

How do I check if C_QTY is not 0 (zero) and if it is 0, treat it as 1 so that multiplication of (A_QTY, B_QTY, C_QTY) should not become 0.

I need to do this inside the select statement only.

Please help.

Thx!!
Reply With Quote
  #2 (permalink)  
Old 07-21-10, 12:18
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
replace C_QTY with "case C_QTY when 0 then 1 else C_QTY end"
Reply With Quote
  #3 (permalink)  
Old 07-21-10, 15:58
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,195
Although it is not a subject of this topic, are more than three parameters for MULTIPLY_ALT supported?
MULTIPLY_ALT(A_QTY, B_QTY, C_QTY)

I couldn't find that support on manuals(DB2 for LUW, z/OS and iSeries).
Also, I tried on DB2 9.7.2 on Windows and failed:
Code:
------------------------------ Commands Entered ------------------------------
VALUES MULTIPLY_ALT(1 , 2 , 3);
------------------------------------------------------------------------------
SQL0440N  No authorized routine named "MULTIPLY_ALT" of type "FUNCTION" having 
compatible arguments was found.  SQLSTATE=42884

------------------------------ Commands Entered ------------------------------
VALUES MULTIPLY_ALT(1 , 2);
------------------------------------------------------------------------------

1                       
------------------------
                      2.

  1 record(s) selected.


------------------------------ Commands Entered ------------------------------
VALUES MULTIPLY_ALT( MULTIPLY_ALT(1 , 2) , 3 );
------------------------------------------------------------------------------

1                                
---------------------------------
                               6.

  1 record(s) selected.
Reply With Quote
  #4 (permalink)  
Old 07-21-10, 18:04
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Thumbs down

Why you have to use the function, if simple multiplication working much faster ?
Check this query (no case was used):


Code:
SELECT SUM( A_QTY * B_QTY * ifnull( nullif(C_QTY, 0),  1) ) 
from CUST_QUANTITY;
Lenny
Reply With Quote
  #5 (permalink)  
Old 07-22-10, 01:48
umayer umayer is offline
Registered User
 
Join Date: Dec 2005
Posts: 273
Lenny77's query may return an incorrect result if C_QTY is nullable, as each occurence of a NULL-value will be replaced by 1
Reply With Quote
  #6 (permalink)  
Old 07-22-10, 11:04
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Lightbulb umayer, you are right. It was just an idea.

Quote:
...multiplication of (A_QTY, B_QTY, C_QTY) should not become 0...
So

Code:
select SUM(case when  A_QTY * B_QTY * C_QTY = 0 then 1 
                else  A_QTY * B_QTY * C_QTY
           end )
from CUST_QUANTITY;
will be a query we are looking for

Lenny
Reply With Quote
Reply

Tags
replace 0

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On