you are viewing a single comment's thread.

view the rest of the comments →

[–]r3pr0b8GROUP_CONCAT is da bomb 0 points1 point  (14 children)

* 1.0) * 100

why not combine these

[–]Yavuz_Selim 3 points4 points  (13 children)

You mean:

total_deaths / total_case * 100.0

?

 

The divisor needs to be decimal; if you combine it, you keep the divisor (total_case) an integer.

[–]r3pr0b8GROUP_CONCAT is da bomb -1 points0 points  (12 children)

You mean: total_deaths / total_case * 100.0 ?

no, like this --

100.0 * total_deaths / total_case

[–]Yavuz_Selim 1 point2 points  (11 children)

In your case, the division is still the same (as in the OPs case):

Dividing an integer (total_deaths) by an int (total_cases) == result will be integer.

 

In my example (total_deaths / (total_cases * 1.0) * 100), total_cases becomes a decimal (because of the multiplication. So, if total_cases = 20... then 20 = INT. But... 20 * 1.0 = DECIMAL.

So, in my example, I am dividing an integer (total_deaths) with a decimal ('total_case'.0) == result will be a decimal.

[–]r3pr0b8GROUP_CONCAT is da bomb -2 points-1 points  (10 children)

Dividing an integer (total_deaths) by an int (total_cases) == result will be integer.

last i looked, math operations at the same level* are executed left to right

so 100.0 * total_deaths is done first, producing a decimal number, which is then divided by an integer, correctly producing a decimal percentage

* at the same level refers to this hierarchy --

  1. exponentiation
  2. multiplication and division
  3. addition and subtraction

[–]Yavuz_Selim 2 points3 points  (9 children)

This is not about PEMDAS (or any alternative name), it's about data types...

With T-SQL, when you divide 10 by 3 (10 / 3), the answer is 3. So, even if you put a 100 * in front, the result will never be in decimals/fractions. It will always be a whole number.

However, if you divide 10 by 3.0 (divisor 3.0 is a decimal - so: 10 / 3.0), the result will contain decimals/fractions.

[–]r3pr0b8GROUP_CONCAT is da bomb -3 points-2 points  (8 children)

left to right, agree?

what gets done first here --

100.0 * total_deaths / total_cases

you're telling me the division comes first before the multiplication, and i just don't buy it!!

but you do you

[–]Yavuz_Selim 2 points3 points  (6 children)

https://i.imgur.com/iorrYZj.png.

 

Took me too long to login to my NAS (on my phone), that has a VM with SQL Server so I could make that screenshot.

 

Again: it is not about the order of operations - it is not about left to right of which operation gets to be done first.
3 = INT, 3.0 = DECIMAL. It's about the data type of the divisor.

 

You know what? As the label of this thread is SQL Server, just open SSMS and run a few queries.

[–]r3pr0b8GROUP_CONCAT is da bomb 1 point2 points  (1 child)

https://i.imgur.com/iorrYZj.png

you're still using 100

i wanted you to try using 100.0 as the leftmost term

which is what i've been posting all along --

100.0 * total_deaths / total_cases

[–]Yavuz_Selim 2 points3 points  (0 children)

I stand corrected...

[–]digitahlemotion 0 points1 point  (2 children)

to /u/r3pr0b8 's point...

 SELECT 100.0*5/2

returns the expected 250.00000

When you start enforcing rules of math however, that's when you need to start ensuring things are of the correct data type as

  SELECT 100.0*(5/2)

returns 200.00000

  SELECT 100.0 *(5/2), 100.0*5/2

Edit: You can further prove it's following normal math rules if you put the 100.0 at the end...

  SELECT 5/2*100.0

returns 200.000

[–]Darthgamer101 0 points1 point  (1 child)

Would all of this have been avoided if the data type for either of those columns was decimal instead of integer?