合并添加额外字符

Coalesce Adding Extra Characters

我有以下值:

table_1:

value
123

table_2:

value
456


select 
t1.value as t1_value
,t2.value as t2_value
,coalesce(t1.value,t2.value,null) as coalesced_value
from
    table_1 as t1
left join
    table_2 as t2
    on
      t1.it_does_not_matter = t2.it_does_not_matter


Output:

t1_value     t2_value     coalesced_value
123          456          123.00000

为什么最后会有额外的 0 值?我该怎么做,所以我的输出是这样的:

Desired Output:
t1_value     t2_value     coalesced_value
123          456          123

对于初学者来说,合并为 NULL 是没有意义的,除非你只是想证明它是神奇的第三个值。

coalesce(t1.value,t2.value,null)

但在雪花

WITH a as ( select 123::number a ), b as (select 456 as b)
SELECT a.a, b.b, coalesce(a.a, b.b, null)
FROM a LEFT JOIN b ON true;    

给出:

A   B   COALESCE(A.A, B.B, NULL)
123 456 123

将秒改为浮动

WITH a as ( select 123::number a ), b as (select 456.1 as b)
SELECT a.a, b.b, coalesce(a.a, b.b, null)
FROM a LEFT JOIN b ON true;    

给出:

A   B       COALESCE(A.A, B.B, NULL)
123 456.1   123.0

但将 float 转换为 double

WITH a as ( select 123::number a ), b as (select 456.1::double as b)
SELECT a.a, b.b, coalesce(a.a, b.b, null)
FROM a LEFT JOIN b ON true;    

给出:

A   B       COALESCE(A.A, B.B, NULL)
123 456.1   123

所以我质疑你的两个表的类型是什么.. 如果你希望它始终是一个数字,只需转换合并即可。

coalesce(a.a, b.b, null)::number

如果您的数据是可变的,并且其他列是浮动的,因此它会将其全部视为浮动,但您只看到一行,则可能会发生这种情况。