Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Divide by zero error encountered.

Author  Topic 

JJ297
Aged Yak Warrior

940 Posts

Posted - 2011-02-08 : 10:50:06
What am I missing. I have this set up exactly like another stored procedure (pivot table) only had to change the names and getting

Divide by zero error encountered. Please look at the bold area to see what I'm missing:



DECLARE @pivot_cols NVARCHAR(MAX);
SELECT @pivot_cols =
STUFF((SELECT
',SUM(CASE WHEN year_month = ''' + year_month +
''' THEN icnt END) AS [' + year_month + ' icnt]' +
',SUM(CASE WHEN year_month = ''' + year_month +
''' THEN ncnt END) AS [' + year_month + ' ncnt]' +
',SUM(CASE WHEN year_month = ''' + year_month +
''' THEN tcnt END) AS [' + year_month + ' tcnt]' +
',CAST((SUM(CASE WHEN year_month = ''' + year_month +
''' THEN icnt+0.00 END)/ SUM(CASE WHEN year_month = ''' + year_month +
''' THEN tcnt+0.00 END))*100 as decimal(4, 1)) AS [' + year_month + ' pcnt]'

FROM (SELECT DISTINCT RIGHT(CONVERT(VARCHAR(9),
CAST(dowrdt AS DATETIME), 6), 6),
CONVERT(CHAR(7), CAST(dowrdt AS DATETIME),
126)
FROM vw_iclaimsTotals) AS T(year_month, sort)
ORDER BY sort
FOR XML PATH('')
), 1, 1, '');


DECLARE @pivot_query NVARCHAR(MAX);
SET @pivot_query =
N'SELECT sort, reg, ' + @pivot_cols +
N' ,rpt, region, area, dist, doc ' +
N' INTO iClaimsPivot ' +
N'FROM (SELECT sort, reg, rpt, region, area, dist, doc, icnt, ncnt, tcnt, pcnt, dowrdt, ' +
N'RIGHT(CONVERT(VARCHAR(9), CAST(dowrdt AS DATETIME),
6),
6) AS year_month ' +

N'FROM vw_iclaimsTotals) AS F ' +
N'GROUP BY sort, reg, area, dist, doc, region, rpt ' +
N'ORDER BY sort;';
Execute(@pivot_query);

JJ297
Aged Yak Warrior

940 Posts

Posted - 2011-02-08 : 11:18:17
Never mind I got it!!!!

I changed this line:

CAST((SUM(CASE WHEN year_month = ''' + year_month +
''' THEN icnt+0.00 END)/ SUM(CASE WHEN year_month = ''' + year_month +
''' THEN NULLIF(tcnt,0) END))*100 as decimal(4, 1)) AS [' + year_month + ' pcnt]'

You see guys I'm learning thanks again for everyone's help.
Go to Top of Page
   

- Advertisement -