473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

COALESCE with MIN

Hello all,

I' trying to query a table(detailed) which has two columns of which one is numerator and the other is denominator part of a field.
I get '0' for denominator for some rows and as it returns NULL I get an error "divide by zero".
So, I'm using a CASE statement like
Expand|Select|Wrap|Line Numbers
  1. CASE WHEN [denominator]= '0'
  2. THEN '0'
  3. ELSE MIN([numerator]/[denominator]) END AS'MIN([numerator]/[denominator])'
  4. ,CASE WHEN [denominator]= '0'
  5. THEN '0'
  6. ELSE MAX([numerator]/[denominator]) END AS'MAX([numerator]/[denominator])'
  7. ,CASE WHEN [denominator]= '0'
  8. THEN '0'
  9. ELSE AVG([numerator]/[denominator]) END AS'AVG([numerator]/[denominator])'
  10.  
  11.  
Now I have to involve my [denominator] into an aggregate function in the GROUP BY clause,which I donot want.
I know this way of writing code is a bit DUMB because I have about 60 more columns on which I have to do this.

So, I was wondering if a COLAESCE function can be used here as
Expand|Select|Wrap|Line Numbers
  1. MIN(COLASCE([numerator]/[denominator])) AS 'MIN([numerator]/[denominator])'
  2.  
I need some guidance in making this code better and workable.
Thanks in advance for your time and suggestions.

-Cinnamongirl.
Feb 13 '12 #1
5 7872
Rabbit
12,516 Expert Mod 8TB
Coalesce will work if the denominator is null, not if it's 0.
Feb 13 '12 #2
ck9663
2,878 Expert 2GB
Try using a CTE instead.


~~ CK
Feb 13 '12 #3
How do I use CTE in a recursive manner for other columns while aggregating the table grouping on hour or day or week?
Feb 14 '12 #4
ck9663
2,878 Expert 2GB
Here, try this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. with prepared_data
  3. as
  4. (
  5.    select 
  6.       numerator, denominator, 
  7.       fraction = 
  8.          case 
  9.             when denominator = 0 then 0.00                        
  10.             else numerator/denominator
  11.          end
  12.    from YourTable  
  13. )
  14. select
  15.    min(fraction), max(fraction), avg(fraction)
  16. from prepared_data
  17.  
  18.  
Remember, all the function you use ignores NULL, by default.

Also if both numerator and denominator are integer, the quotient will be integer as well.

Happy Coding!!!


~~ CK
Feb 15 '12 #5
It works...Thank you CK
Feb 15 '12 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Bob Bedford | last post by:
I've a query like this: select coealesce(table1.code1, table2.code1) as mycode from... where mycode = 'XXX' But this query fails. Unknow column mycode in where clause. why ??? "coalesce()...
2
by: Jeff Roughgarden | last post by:
I am using a clever product called SQL Expert Pro to optimize problematic SQL statements. It works by generating syntactically identical variations of the original SQL, finding all unique execution...
3
by: Steffen Vulpius | last post by:
Hi everybody, VARCHAR has a higher precedence than CHAR. If you have a query SELECT COALESCE(c1,c2) FROM T1 where c1 is CHAR(5) and c2 is VARCHAR(10), I would expect the return type to be...
5
by: John Greve | last post by:
Any ideas on why my the result 'green' row column DELTA does not yield -1 for? That is not how I expected COALESCE( ... ) to work. Every other row for DELTA (including 'black') comes out the...
3
by: darrel | last post by:
I have three tables making a many-to-many relationship: Companies CoID | CoName Link CoID | InsID Insurers InsID | InsName
4
by: Don | last post by:
If I have an SQL query which returns an aggregate of several decimal fields like so: (sum(COALESCE(myDecimal1, 0)+ sum(COALESCE(myDecimal2, 0)+ sum(COALESCE(myDecimal3, 0)) as MyTotal I get...
10
by: db2group88 | last post by:
hi, we are using db2 v8.2 EE on windows, with jcc driver. try to create a join view, after that i check the syscat.columns table, for those decimal field use COALESCE method, all the length become...
2
by: raysefo | last post by:
Hi, I m trying to use coalesce but i m getting this error below, btw i m using db2 8.2. what i m doing is; .... where eh.toblm = coalesce(NULL,eh.toblm) SQL0206N "NULL " is not valid in the...
1
by: r035198x | last post by:
This is a short tip describing the sql coalesce function. Description COALESCE accepts a comma separated list of parameters and returns the first value that is not null in that list or null...
1
by: =?Utf-8?B?R1ROMTcwNzc3?= | last post by:
Hi All, is this the correct way to Coalesce a session var - <%Session("EMPLOYETOKENS") = tokens.Fields.Item(COALESCE(SUM("JBCLTokens"),0)).Value%? Thank you
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.