March 6, 2017 at 7:29 am
Hello
I am trying to get an output that will show me:
Total number of deliveries missed / total deliveries expressed as a %
So I have scripted this as:
SUM(a.fieldmissed) / SUM(a.fieldtotal) *100 AS [Deliveries Missed]
But I am getting a return of 0 (zero) when the values are say 200 / 14000, when I'm hoping it should say 1.4.
Please can someone help point out what I'm doing wrong? I feel I'm missing a function somewhere.
Thanks.
March 6, 2017 at 7:34 am
faulknerwilliam2 - Monday, March 6, 2017 7:29 AMHello
I am trying to get an output that will show me:
Total number of deliveries missed / total deliveries expressed as a %
So I have scripted this as:
SUM(a.fieldmissed) / SUM(a.fieldtotal) *100 AS [Deliveries Missed]
But I am getting a return of 0 (zero) when the values are say 200 / 14000, when I'm hoping it should say 1.4.
Please can someone help point out what I'm doing wrong? I feel I'm missing a function somewhere.
Thanks.
Use CAST( SUM(a.fieldmissed) AS decimal(18,8)) / SUM(a.fieldtotal) *100 AS [Deliveries Missed]
You're being victim of something called "integer division". This is an expected functionality but can take you by surprise if you don't pay attention or know how it works.
March 6, 2017 at 7:40 am
Thanks for the prompt and accurate response. Much appreciated.
March 6, 2017 at 8:28 am
Luis is spot on about how this occurred. As a simplification of what he posted, unless the SUM of a.fieldmissed will create a number larger than 13 significant digits, you can do this without explicit casting. Move the "100" and change it to have a decimal place.SUM(a.fieldmissed)*100.0 / SUM(a.fieldtotal) AS [Deliveries Missed]
--Jeff Moden
Change is inevitable... Change for the better is not.
March 7, 2017 at 2:39 am
Jeff Moden - Monday, March 6, 2017 8:28 AMLuis is spot on about how this occurred. As a simplification of what he posted, unless the SUM of a.fieldmissed will create a number larger than 13 significant digits, you can do this without explicit casting. Move the "100" and change it to have a decimal place.SUM(a.fieldmissed)*100.0 / SUM(a.fieldtotal) AS [Deliveries Missed]
Thanks for taking the trouble to reply Jeff. Your solution works well as well.
Just to push my luck a bit, but do you know if there's a way of restricting the output to two decimal places? I know I can tidy up decimal places in reporting services, but would be handy to do in the SQL as well.
Thanks.
March 7, 2017 at 2:49 am
Cast as something like decimal(7,2). Don't take my word for that, though - make sure you read and understand the documentation about the decimal data type.
John
March 7, 2017 at 2:51 am
CAST(<Calculation> AS NUMERIC(6, 2))
I'm a DBA.
I'm not paid to solve problems. I'm paid to prevent them.
March 7, 2017 at 3:54 am
andrew gothard - Tuesday, March 7, 2017 2:50 AMCAST(<Calculation> AS NUMERIC(6, 2))
That did the trick:
CAST(SUM(a.fieldmissed)*100.0 / SUM(a.fieldtotal) AS numeric (10,2))
Many thanks
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply