简体   繁体   English

如何划分两列?

[英]How to divide two columns?

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:我试图从连接的表中划分两列,但结果(列 relative_duration 的值)始终为 0。查询如下:

    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?有谁知道上面的查询可能有什么问题以及如何修复它以便将表 t1 中的列 total_duration 与表 t2 中的列 total_events_duration 分开?

BTW I tried to replace division with subtraction ("/" with "-") and in that case the column relative_duration is not 0.顺便说一句,我试图用减法(“/”和“-”)替换除法,在这种情况下,relative_duration 列不是 0。

Presumably, those columns are integer columns - which will be the reason as the result of the calculation will be of the same type.据推测,这些列是整数列 - 这将是计算结果将具有相同类型的原因。

eg if you do this:例如,如果你这样做:

SELECT 1 / 2

you will get 0, which is obviously not the real answer.你会得到 0,这显然不是真正的答案。 So, convert the values to eg decimal and do the calculation based on that datatype instead.因此,将值转换为例如十进制并根据该数据类型进行计算。

eg例如

SELECT CAST(1 AS DECIMAL) / 2

gives 0.500000给出 0.500000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM