【问题标题】:Coalesce Adding Extra Characters合并添加额外字符
【发布时间】:2021-03-17 04:44:39
【问题描述】:

我有以下价值观:

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

【问题讨论】:

    标签: sql snowflake-cloud-data-platform coalesce


    【解决方案1】:

    对于初学者来说,合并为 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
    

    但将浮点数转换为双精度

    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
    

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

    【讨论】:

      猜你喜欢
      • 2021-06-22
      • 2013-08-30
      • 1970-01-01
      • 1970-01-01
      • 2018-05-10
      • 1970-01-01
      • 1970-01-01
      • 2019-12-09
      • 1970-01-01
      相关资源
      最近更新 更多