Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Divide By Zero error when I use a case statement Expand / Collapse
Author
Message
Posted Monday, October 21, 2013 12:21 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Today @ 7:27 AM
Points: 331, Visits: 750
I'm getting a divie by zero error with the following query. Can someone tell me what Im doing wrong?


SELECT ForcastUnitsTimesOMSCost, case when IsNUll(ForcastUnitsTimesOMSCost,0.00) < 0 then SUM(CTCBOHAndWCSBOHTimesOMSCost/ForcastUnitsTimesOMSCost) else 0 end as Calculation
FROM MyTable
Where (CONVERT(date, GETDATE()) BETWEEN EventStartDate AND EventEndDate)
Group By ForcastUnitsTimesOMSCost
Post #1506787
Posted Monday, October 21, 2013 12:36 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Today @ 7:27 AM
Points: 331, Visits: 750
My bad I used the wrong query:





SELECT ForcastUnitsTimesOMSCost, case when IsNUll(ForcastUnitsTimesOMSCost,0.00) <> 0 then SUM(CTCBOHAndWCSBOHTimesOMSCost/ForcastUnitsTimesOMSCost) else 0 end as Calculation 
FROM MyTable
Where (CONVERT(date, GETDATE()) BETWEEN EventStartDate AND EventEndDate)
Group By ForcastUnitsTimesOMSCost

Post #1506795
Posted Monday, October 21, 2013 12:37 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Today @ 7:27 AM
Points: 331, Visits: 750
ForcastUnitsTimesOMSCost is a float datatype
Post #1506796
Posted Monday, October 21, 2013 12:39 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:54 PM
Points: 13,471, Visits: 12,329
You are mixing up your aggregates and scalar values. You are trying to use a case expression to control the whole column. This doesn't work at all. It is awfully hard to figure out what you are trying to do here but here is my shot in the dark.

SELECT ForcastUnitsTimesOMSCost,  SUM(CTCBOHAndWCSBOHTimesOMSCost/IsNUll(ForcastUnitsTimesOMSCost, 0.00)) as Calculation
FROM MyTable
Where (CONVERT(date, GETDATE()) BETWEEN EventStartDate AND EventEndDate)
Group By ForcastUnitsTimesOMSCost
having IsNUll(ForcastUnitsTimesOMSCost, 0.00) < 0

If that doesn't help you will need to provide us a few things:

1. Sample DDL in the form of CREATE TABLE statements
2. Sample data in the form of INSERT INTO statements
3. Expected results based on the sample data

Please take a few minutes and read the first article in my signature for best practices when posting questions.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1506799
Posted Monday, October 21, 2013 12:43 PM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 12:39 AM
Points: 3,136, Visits: 11,494
Try this:
SELECT
ForcastUnitsTimesOMSCost,
SUM(isnull(CTCBOHAndWCSBOHTimesOMSCost/nullif(ForcastUnitsTimesOMSCost,0.00),0.00)) as Calculation
FROM
MyTable
Where
(CONVERT(date, GETDATE()) BETWEEN EventStartDate AND EventEndDate)
Group By
ForcastUnitsTimesOMSCost

Post #1506802
Posted Monday, October 21, 2013 1:40 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Today @ 7:27 AM
Points: 331, Visits: 750
That worked, thanks
Post #1506840
Posted Monday, October 21, 2013 1:42 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:54 PM
Points: 13,471, Visits: 12,329
dndaughtery (10/21/2013)
That worked, thanks


Which one?


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1506844
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse