df-drills pandas · Polars · DuckDB SQL · DuckDB Relational 题目列表 登录

多列取第一个非空

简单 05-null 缺失值 Nonecoalesce

每个联系人有手机、家庭、办公三个电话字段。按 mobile → home → office 的优先级,取第一个不为 null 的号码作为 phone。

  • 三个字段都是 null 时,phone 为 null。
  • 空字符串 "" 不是 null,它就是一个“非空值”,会被选中。

输入

  • contacts(id, mobile, home, office),三个电话字段都是字符串。

输出

列为 id, phone,每个联系人一行,行顺序不限。

输入表 (case base,共 2 个 case:base、edge)

contacts local/base/contacts
#idint64mobilestringhomestringofficestring
1 1 13800000001 010-1111 010-9001
2 2 null 010-2222 010-9002
3 3 null null 010-9003
4 4 13800000004 null null
5 5 null 010-5555 null
6 6 13800000006 010-6666 010-9006
7 7 null null null
8 8 13800000008 null 010-9008
共 8 行

期望输出 (case base,行顺序不限)

#idInt64phoneString
1 1 13800000001
2 2 010-2222
3 3 010-9003
4 4 13800000004
5 5 010-5555
6 6 13800000006
7 7 null
8 8 13800000008
共 8 行