Solved

How to Avoid Divide by Zero in a Calculated Column

Posted on 2007-03-27
6
1,160 Views
Last Modified: 2012-06-22
I am trying to protect a divide by zero in my last column.

SELECT
max(dbo.getmonthdate(t.TransDate)),
max(dbo.getmonth(t.TransDate)),
Sum(CASE t.TransType WHEN 1 THEN 1 ELSE 0 END) AS PassportsIssued,
Sum(CASE t.TransType WHEN 7 THEN 1 ELSE 0 END) AS PassportVerifications,
Sum(CASE t.TransType WHEN 8 THEN 1 ELSE 0 END) AS SkillVerifications,(Sum(CASE t.TransType WHEN 7 THEN 1 ELSE 0 END) + Sum(CASE t.TransType WHEN 8 THEN 1 ELSE 0 END))/Sum(CASE t.TransType WHEN 1 THEN 1 ELSE 0 END) AS Calculated

The problem is that
(CASE t.TransType WHEN 1 THEN 1 ELSE 0 END)
may sometimes be zero in which case I need to return "No Ratio Available"

Can Anyone help? I understand I may have to use a nested case statement or something!
0
Comment
Question by:MayoorPatel
6 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 168 total points
ID: 18798906
Select MaxMonthDate,MaxMonth,PassportsIssued,PassportVerifications,SkillVerifications, Case When PassportsIssued = 0 Then 'No Ratio Available' Else Cast((PassportVerifications + SkillVerifications) / PassportsIssued As Varchar(20)) End As Calculated From
(
SELECT
max(dbo.getmonthdate(t.TransDate)) As MaxMonthDate,
max(dbo.getmonth(t.TransDate)) As MaxMonth,
Sum(CASE t.TransType WHEN 1 THEN 1 ELSE 0 END) AS PassportsIssued,
Sum(CASE t.TransType WHEN 7 THEN 1 ELSE 0 END) AS PassportVerifications,
Sum(CASE t.TransType WHEN 8 THEN 1 ELSE 0 END) AS SkillVerifications,(Sum(CASE t.TransType WHEN 7 THEN 1 ELSE 0 END) + Sum(CASE t.TransType WHEN 8 THEN 1 ELSE 0 END))/Sum(CASE t.TransType WHEN 1 THEN 1 ELSE 0 END) AS Calculated
From MyTable) Calculation

For example, doing a secondary select means that you can work with the totals without having to recalculate them.
0
 
LVL 50

Assisted Solution

by:Lowfatspread
Lowfatspread earned 166 total points
ID: 18799195
like this...

Select MaxMonthDate,MaxMonth,PassportsIssued,PassportVerifications,SkillVerifications
     , Case When PassportsIssued = 0 Then 'No Ratio Available'
            Else Convert(varchar(30),passportVerifications + (skillverifications * 1.00/passportsissued))
            End As Calculated
  From
(
SELECT
max(dbo.getmonthdate(t.TransDate)) As MaxMonthDate,
max(dbo.getmonth(t.TransDate)) As MaxMonth,
Sum(CASE t.TransType WHEN 1 THEN 1 ELSE 0 END) AS PassportsIssued,
Sum(CASE t.TransType WHEN 7 THEN 1 ELSE 0 END) AS PassportVerifications,
Sum(CASE t.TransType WHEN 8 THEN 1 ELSE 0 END) AS SkillVerifications
From MyTable) as X
0
 
LVL 11

Assisted Solution

by:Ved Prakash Agrawal
Ved Prakash Agrawal earned 166 total points
ID: 18799208
SELECT
max(dbo.getmonthdate(t.TransDate)),
max(dbo.getmonth(t.TransDate)),
Sum(CASE t.TransType WHEN 1 THEN 1 ELSE 0 END) AS PassportsIssued,
Sum(CASE t.TransType WHEN 7 THEN 1 ELSE 0 END) AS PassportVerifications,
Sum(CASE t.TransType WHEN 8 THEN 1 ELSE 0 END) AS SkillVerifications,
CASE WHEN t.TransType =1 THEN  CAST((Sum(CASE t.TransType WHEN 7 THEN 1 ELSE 0 END) + Sum(CASE t.TransType WHEN 8 THEN 1 ELSE 0 END))/Sum(CASE t.TransType WHEN 1 THEN 1 ELSE 0 END)  as varchar(2000) ) ELSE 'No Ratio Available' END AS Calculated
0
 
LVL 1

Author Comment

by:MayoorPatel
ID: 18800409
Hi there I forgot to add that there was this in the from clause

FROM GetCardtypes(3) ct inner join
tblTransactions t
on ct.cardtypeID=t.cardTypeID
inner join tblUsers u on
      u.PHID = t.IssuedToID

Can anyone help add this to the solutions you have provided.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now