Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dividing 2 numbers in Sql Server

Tags:

sql

sql-server

I am doing SQL Server query calculations and the division always gives me zero.

SUM(sl.LINES_ORDERED) 
, SUM(sl.LINES_CONFIRMED)
, SUM(sl.LINES_CONFIRMED) / SUM(sl.LINES_ORDERED) AS 'Percent'

The sample dataset returned is:

enter image description here

In this example, the third row should have a division value of 1.02, but it's shown as zero. Why is that?

like image 909
Justin Samuel Avatar asked Aug 16 '12 10:08

Justin Samuel


Video Answer


1 Answers

try

SUM(sl.LINES_CONFIRMED) * 1.0 / SUM(sl.LINES_ORDERED)

An integer devision can only return full numbers and not floating point numbers. You can force a floating point division like in the example above.

like image 147
juergen d Avatar answered Sep 19 '22 15:09

juergen d