Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to divide two columns?

Tags:

sql

I tried to divide two columns from joined tables but the result (value of column relative_duration) is always 0. The query is the following:

    SELECT t1.[user_1]
      ,t1.[user_2]
      ,t1.[total_duration]
      ,(t1.total_duration/t2.[total_events_duration]) AS relative_duration
  FROM [CDRs].[dbo].[aggregate_monthly_events] AS t1 INNER JOIN [CDRs].[dbo].[user_events_monthly_stats] AS t2 ON t1.[user_1] = t2.[user_1]

Does anyone know what could be wrong in the query above and how to fix it in order to divide column total_duration from table t1 with column total_events_duration from table t2?

BTW I tried to replace division with subtraction ("/" with "-") and in that case the column relative_duration is not 0.

like image 490
Niko Gamulin Avatar asked Oct 22 '10 12:10

Niko Gamulin


People also ask

How do I divide two columns of numbers in Excel?

To divide columns in Excel, just do the following: Divide two cells in the topmost row, for example: =A2/B2. Insert the formula in the first cell (say C2) and double-click the small green square in the lower-right corner of the cell to copy the formula down the column. Done!

How do you divide two values in Excel?

Type the equal sign, and use cell references instead of typing regular numbers. For instance, if you want to divide cell A1 by cell B1, type =A1/B1. If you want to divide a cell by a constant number such as 5, you would type =A1/5. When you press enter, Excel displays the result in the cell.

How do I divide two columns in pandas?

Example 1:The simple division (/) operator is the first way to divide two columns. You will split the First Column with the other columns here. This is the simplest method of dividing two columns in Pandas. We will import Pandas and take at least two columns while declaring the variables.


1 Answers

Presumably, those columns are integer columns - which will be the reason as the result of the calculation will be of the same type.

e.g. if you do this:

SELECT 1 / 2

you will get 0, which is obviously not the real answer. So, convert the values to e.g. decimal and do the calculation based on that datatype instead.

e.g.

SELECT CAST(1 AS DECIMAL) / 2

gives 0.500000

like image 74
AdaTheDev Avatar answered Sep 21 '22 18:09

AdaTheDev