我有两张 table
TB_1 TB_3
Month Total Month Total
2012-01 6 2012-01 12
2013-02 6 2013-02 12
2014-03 10 2014-03 20
2015-04 10 2015-04 20
在结果表中我需要遵循结果:
RESULT_TB
Month Total
2012-01 2
2013-02 2
2014-03 2
2015-04 2
我尝试了以下方法:
Select TB_3.total / TB_1.total
From TB_3, TB_1
但是它不起作用,请告诉我,怎么办?
最佳答案
试试这个:
select t1.month,
case
when t1.total = 0
then null
else t2.total / t1.total
end total
from tb_1 t1
join tb_3 t2 on t1.month = t2.month
我使用 CASE
来确保 tb_1 表中的总数是否为零,输出为 NULL
而不是抛出 除以零错误。
关于sql - 将一个表中的列划分为另一个表中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42189578/