How to split two columns?

I tried to separate two columns from the joined tables, but the result (column relative_duration value) is always 0. The query is as follows:

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 might be wrong in the above query and how to fix it to split the total_duration column from table t1 into the total_events_duration column from table t2?

BTW I tried to replace the division by subtraction ("/" by "-"), in which case the relative length of the column is not 0.

+4
source share
1 answer

Presumably, these columns are whole columns, which will cause the result of the calculation to be of the same type.

eg. if you do this:

 SELECT 1 / 2 

you get 0, which is obviously not the real answer. Thus, convert the values, for example. decimal and perform calculations based on this data type.

eg.

 SELECT CAST(1 AS DECIMAL) / 2 

gives 0.500000

+22
source

All Articles