| Author |
Topic |
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2012-01-25 : 09:55:03
|
| This isn't working what am I doing wrong. Getting incorrect syntax near *select regionname, ((pend300 + pend351 + pend400) / * 100) as percfrom t16report |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-25 : 09:57:26
|
might be this? you're missing some operand in between tooselect regionname, ((pend300 + pend351 + pend400) /(something missing here) * 100.0) as percfrom t16report ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2012-01-25 : 10:02:04
|
| Yes I was thanks!Added this:select reg, ((pend300 + pend351 + pend400) /(pendfo300) * 100.0) as percfrom t16reportNow I'm betting Divide by zero error encountered. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-25 : 10:08:18
|
thats because you've some rows with 0 in denominatorselect reg, ((pend300 + pend351 + pend400) /(NULLIF(pendfo300,0)) * 100.0) as percfrom t16report ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2012-01-25 : 10:24:21
|
Correct as usual I changed the query to this:select distinct dist, ((pendinfo) /(nullif(pendfo300, 0)) * 100.0) as percfrom t16reportwhere reg='c' and area='05'order by dist This works but displays as this. How do I get all of the 201, 210 etc... to add up.Dist perc201 NULL201 3200.0201 9400.0210 NULL210 600.0210 800.0210 2000.0226 NULL226 1300.0226 2900.0231 NULL231 2100.0305 NULL305 1500.0306 NULL308 NULL309 NULL309 500.0313 900.0313 2900.0313 3000.0 |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2012-01-25 : 10:27:09
|
never mind I got this to work:select distinct dist, sum((pendinfo) /(nullif(pendfo300, 0)) * 100.0) as percfrom t16reportwhere reg='c' and area='05'group by distorder by dist dist perc201 12600.0210 3400.0226 4200.0231 2100.0305 1500.0306 NULL308 NULL309 500.0How can I add 0 if there's NULL and get the numbers to format better?313 6800.0 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-25 : 10:44:01
|
| [code]select distinct dist, isnull(sum((pendinfo) /(nullif(pendfo300, 0)) * 100.0),0) as percfrom t16reportwhere reg='c' and area='05'group by distorder by dist[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2012-01-25 : 10:51:02
|
Thanks again! |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2012-01-25 : 11:05:21
|
I'm back...Trying to get the numbers to come out as 12.6 etc...I added this:select distinct dist, isnull(sum((pendinfo) /(nullif(convert(decimal,pendfo300, 0)) * 100.0),0) as percfrom t16reportwhere reg='c' and area='05'group by distorder by distError is:Incorrect syntax near ')'. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-25 : 11:43:40
|
try belowselect distinct dist, isnull(sum((pendinfo) /nullif((convert(decimal(precisionvalue,scalevalue),pendfo300, 0) * 100.0),0)),0) as percfrom t16reportwhere reg='c' and area='05'group by distorder by dist ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2012-01-25 : 12:13:32
|
| Tried it but getting:Incorrect syntax near ','. |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2012-01-25 : 15:38:45
|
Figured it out...Is there another way to do this statement instead of using the Case statement?SELECT sort='000', RegionAcronym as Dist,'' AS reg, RegionAcronym, '' area, --'<a href=region.aspx?Reg=' + Reg + '>' + RegionAcronym + '</a>' as dist, SUM(totpenfodds) AS 'totpenfodds', SUM(pendinfo) AS 'pendinfo', SUM(pendndds) AS 'pendndds', SUM(pendfo300) AS 'pendfo300', SUM(penddds300) AS 'penddds300', SUM(pend250) AS 'pend250', SUM(pend300) AS 'pend300', SUM(pend351) AS 'pend351', SUM(pend400) AS 'pend400',"perc" = Casewhen sum(pendinfo) = 0 then 0When sum(pendfo300) = 0 then 0Else (convert(decimal(6,2),((((sum(pendfo300)) * 1.00) / sum(pendinfo)) * 100))) EndFROM T16Report where reg='c'group by reg, RegionAcronym |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-25 : 16:45:27
|
| why do you need case here? isnt below enough?isnull((convert(decimal(6,2),((((sum(pendfo300)) * 1.00) / nullif(sum(pendinfo),0)) * 100))),0)------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2012-01-26 : 07:51:44
|
| Yes that's perfect. I didn't know how to set it up with the decimal so that's why I did it with the case statement. Your way of course is much easier. Thanks! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-26 : 08:41:23
|
| welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-26 : 08:46:17
|
quote: Originally posted by X002548
quote: Originally posted by visakh16 thats because you've some rows with 0 in denominatorselect reg, ((pend300 + pend351 + pend400) /(NULLIF(pendfo300,0)) * 100.0) as percfrom t16report ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
That's not CorrectCASE WHEN pendfo300 = 0 THEN 0WHEN pendfo300 IS NULL THEN 0ELSE (pend300 + pend351 + pend400) /(pendfo300,0 * 100.0) END as percBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/
see modified suggestionthats why i've that isnull() bit around this to make it 0------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2012-01-26 : 10:30:11
|
I hate non ANSI StuffDECLARE @t16report table (pendinfo int, pendfo300 int, dist char(1))INSERT INTO @t16report (pendinfo, pendfo300, dist)SELECT 10, 1, 'x' UNION ALLSELECT 10, 2, 'x' UNION ALLSELECT 10, 3, 'x' UNION ALLSELECT 10, 4, 'y' UNION ALLSELECT 10, 0, 'z'select distinct dist, isnull(sum((pendinfo)/ nullif((convert(decimal(15,4),pendfo300, 0)* 100.0),0)),0) as percfrom @t16report--where reg='c' and area='05', pendfo300order by dist Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-26 : 10:37:47
|
quote: Originally posted by X002548 I hate non ANSI StuffDECLARE @t16report table (pendinfo int, pendfo300 int, dist char(1))INSERT INTO @t16report (pendinfo, pendfo300, dist)SELECT 10, 1, 'x' UNION ALLSELECT 10, 2, 'x' UNION ALLSELECT 10, 3, 'x' UNION ALLSELECT 10, 4, 'y' UNION ALLSELECT 10, 0, 'z'select distinct dist, isnull(sum((pendinfo)/ nullif((convert(decimal(15,4),pendfo300, 0)* 100.0),0)),0) as percfrom @t16report--where reg='c' and area='05', pendfo300order by dist Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/
then use COALESCE ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2012-01-26 : 14:24:57
|
| Thanks again! |
 |
|
|
Next Page
|