类别:

半结构化和结构化数据函数 (数组/对象)

OBJECT_CONSTRUCT_KEEP_NULL

返回由实参构造的 OBJECT

另请参阅:

OBJECT_CONSTRUCT

语法

OBJECT_CONSTRUCT_KEEP_NULL( [<key1>, <value1> [, <keyN>, <valueN> ...]] )

OBJECT_CONSTRUCT_KEEP_NULL( * )
Copy

返回

返回值的数据类型是 OBJECT

使用说明

  • 该函数接受以下任一内容:

    • 由零个或多个键值对组成的序列(其中键是字符串,值为任意类型)。

    • 星号。

    与星号一同调用时,使用属性名称作为键,使用关联的元组值作为值来构造对象。请参阅以下示例。

  • 如果键为 NULL(即 SQL NULL),则结果对象中将省略键值对。但是,如果值为 null,则保留键值对。

  • 构造的对象不一定保留键值对的原始顺序。

示例

此示例显示了 OBJECT_CONSTRUCT 和 OBJECT_CONSTRUCT_KEEP_NULL 之间的区别:

SELECT OBJECT_CONSTRUCT('key_1', 'one', 'key_2', NULL) AS WITHOUT_KEEP_NULL,
       OBJECT_CONSTRUCT_KEEP_NULL('key_1', 'one', 'key_2', NULL) AS KEEP_NULL_1,
       OBJECT_CONSTRUCT_KEEP_NULL('key_1', 'one', NULL, 'two') AS KEEP_NULL_2
       ;
+-------------------+-------------------+------------------+
| WITHOUT_KEEP_NULL | KEEP_NULL_1       | KEEP_NULL_2      |
|-------------------+-------------------+------------------|
| {                 | {                 | {                |
|   "key_1": "one"  |   "key_1": "one", |   "key_1": "one" |
| }                 |   "key_2": null   | }                |
|                   | }                 |                  |
+-------------------+-------------------+------------------+
Copy

下面的示例还显示了 OBJECT_CONSTRUCT 和 OBJECT_CONSTRUCT_KEEP NULL 之间的区别,但此示例使用一个小表(在查询之前显示):

SELECT *
    FROM demo_table_1
    ORDER BY province;
+------------------+--------------+
| PROVINCE         | CREATED_DATE |
|------------------+--------------|
| Alberta          | 2020-01-19   |
| British Columbia | NULL         |
| Manitoba         | 2020-01-18   |
| NULL             | 2020-01-20   |
+------------------+--------------+
Copy
SELECT OBJECT_CONSTRUCT(*) AS oc,
       OBJECT_CONSTRUCT_KEEP_NULL(*) AS oc_keep_null
    FROM demo_table_1
    ORDER BY oc_keep_null['PROVINCE'];
+----------------------------------+----------------------------------+
| OC                               | OC_KEEP_NULL                     |
|----------------------------------+----------------------------------|
| {                                | {                                |
|   "CREATED_DATE": "2020-01-19",  |   "CREATED_DATE": "2020-01-19",  |
|   "PROVINCE": "Alberta"          |   "PROVINCE": "Alberta"          |
| }                                | }                                |
| {                                | {                                |
|   "PROVINCE": "British Columbia" |   "CREATED_DATE": null,          |
| }                                |   "PROVINCE": "British Columbia" |
|                                  | }                                |
| {                                | {                                |
|   "CREATED_DATE": "2020-01-18",  |   "CREATED_DATE": "2020-01-18",  |
|   "PROVINCE": "Manitoba"         |   "PROVINCE": "Manitoba"         |
| }                                | }                                |
| {                                | {                                |
|   "CREATED_DATE": "2020-01-20"   |   "CREATED_DATE": "2020-01-20",  |
| }                                |   "PROVINCE": null               |
|                                  | }                                |
+----------------------------------+----------------------------------+
Copy

有关使用密切相关函数 OBJECT_CONSTRUCT 的示例,请参阅 OBJECT_CONSTRUCT

语言: 中文