---
url: /v6.2/calculation-function-dataset.md
---
# 数据集函数

此类函数的返回结果都是包含了字段和数据的数据集。 此类函数说明的示例都是基于数据库中的 movie 表，它的访问路径是 test。 下面是 movie 的数据。在衡石系统中新建的数据连接 id 为 1， 新建的应用 id 为1，新建的数据集 id 为1。

| id        | type     |  name   |  votes   |
|-----------|----------|---------| ---------|
| 1         |  动画   | 狮子王     |  8.5 |
| 2         | 喜剧    | 疯狂的外星人  |  8.2   |
| 3         | 动画    | 冰雪奇缘      |  8.1   |
| 4         | 喜剧    | 唐人街探案     |  8.3  |

## app_dataset

此函数返回某个应用下的某个数据集的数据。

* 函数语法

```text
app_dataset({appId}, {datasetId})
```

* 参数说明

  * appId:  必填，应用的 id 。可选类型：NUMBER
  * datasetId:  必填，数据集的 id 。可选类型：NUMBER

* 示例

```text
app_dataset(1, 1)
```

## dataset

此函数返回某个数据集的数据, 这个函数必须在有应用的语境下才可以用。

* 函数语法

```text
dataset({id})
```

* 参数说明

  * id:  必填，数据集的 id 。可选类型：NUMBER

* 示例

```text
dataset(1)
```

## db_source

此函数返回connection下的某个表内容

* 函数语法

```text
db_source({connectionId}, {path}, {tableName})
```

* 参数说明

  * connectionId:  必填，当前系统里配置的数据连接 id。可选类型：NUMBER
  * path:  必填，表的访问路径，要用数据库默认，可以填\[]. 可选类型：STRING 数组
  * tableName: 必填，要查询的表名。可选类型：STRING

* 示例

```text
db_source(1,['test'], 'movie')
```

下面是返回结果：

```json
{
  "data": {
    "data": [
      [
        1,
        "动画",
        "狮子王",
        8.5
      ],
      [
        2,
        "喜剧",
        "疯狂的外星人",
        8.2
      ],
      [
        3,
        "动画",
        "冰雪奇缘",
        8.1
      ],
      [
        4,
        "喜剧",
        "唐人街探案",
        8.3
      ]
    ],
    "schema": [
      {
        "fieldName": "id",
        "visible": true,
        "nativeType": "int4"
      },
      {
        "fieldName": "type",
        "visible": true,
        "nativeType": "text"
      },
      {
        "fieldName": "name",
        "visible": true,
        "nativeType": "text"
      },
      {
        "fieldName": "votes",
        "visible": true,
        "nativeType": "numeric"
      }
    ]
  }
}
```

## filter

此函数返回过滤后的数据集数据。

* 函数语法

```text
filter({dataset expression}, {filter expression})
```

* 参数说明

  * dataset expression:  必填，返回结果是 dataset 的表达式。
  * datasetId:  必填，返回结果是 bool 的表达式。 可选类型： BOOL

* 示例

```text
filter(app_dataset(1, 1), {votes} > 8.2)
```

```json
{
  "data": {
    "data": [
      [
        1,
        "动画",
        "狮子王",
        8.5
      ],
      [
        4,
        "喜剧",
        "唐人街探案",
        8.3
      ]
    ],
    "schema": [
      {
        "fieldName": "id",
        "visible": true,
        "nativeType": "int4"
      },
      {
        "fieldName": "type",
        "visible": true,
        "nativeType": "text"
      },
      {
        "fieldName": "name",
        "visible": true,
        "nativeType": "text"
      },
      {
        "fieldName": "votes",
        "visible": true,
        "nativeType": "numeric"
      }
    ]
  }
}
```

## select_fields

此函数返回数据集中选定的列的数据。每个列的运算都是独立的，不会相互影响。

* 函数语法

```text
select_fields({dataset expression}, {field1}, {field2} ...)
```

* 参数说明

  * dataset expression:  必填，返回结果是 dataset 的表达式, 可选范围是本文中介绍的数据集函数表达式。
  * field1, field2 ...:  必填，参数数量可变，最少为1，描述列的表达式。 参数不可以用数据集中的新增指标。

* 示例1

```text
select_fields(filter(app_dataset(1, 1), {votes} < 8.3), {id}, {type} as {类型}, {votes} - 8 as {分数尾数})
```

下面是返回结果：

```json
{
  "data": {
    "data": [
      [
        1,
        "动画",
        0.5
      ],
      [
        4,
        "喜剧",
        0.3
      ]
    ],
    "schema": [
      {
        "fieldName": "id",
        "visible": true,
        "nativeType": "int4"
      },
      {
        "fieldName": "类型",
        "visible": true,
        "nativeType": "text"
      },
      {
        "fieldName": "分数尾数",
        "visible": true,
        "nativeType": "numeric"
      }
    ]
  }
}
```

* 示例2

```text
select_fields(app_dataset(1, 1), {type}, sum({votes}) as {总票数})
```

下面是返回结果：

```json
{
  "data": {
    "data": [
      [
        "动画",
        33.1
      ],
      [
        "喜剧",
        33.1
      ],
      [
        "动画",
        33.1
      ],
      [
        "喜剧",
        33.1
      ]
    ],
    "schema": [
      {
        "fieldName": "type",
        "visible": true,
        "nativeType": "text"
      },
      {
        "fieldName": "总票数",
        "visible": true,
        "nativeType": "numeric"
      }
    ]
  }
}
```

## select_fields_complete

此函数返回数据集中选定的列的数据。每个列的运算都是独立的，不会相互影响。同 SELECT_FIELDS 不同的是，它可以加入过滤条件、排序、分页信息。

* 函数语法

```text
select_fields_complete({dataset expression},
[{field1},{field2},{field3} ...],
[filter_expr_1,filter_expr_2 ...],
[sort_expr_1,sort_expr_2...],
offset, limit)
```

* 参数说明

  * dataset expression:  必填，返回结果是 dataset 的表达式。
  * \[{field1},{field2},{field3} ...]：必填，数组长度没有限制，数组为空的时候表示获取所有字段的信息。
  * \[filter_expr_1,filter_expr_2 ...]：必填，过滤表达式，不同表达式之间是 and 的关系，为空表示没有过滤条件。
  * \[sort_expr_1,sort_expr_2...]：必填，排序字段，写表达式的场景下，这里不支持指定顺序，都是按照升级排序。但是写 HE 结构体的时候，是可以指定顺序的。
  * offset:  必填，获取数据的偏移量。
  * limit:  必填，或者数据的限制条数。

* 示例1: 写表达式

```text
select_fields_complete(dataset(2),[{id},{month}],[{id}>1,{id}<50],[{id}],6,3)
```

下面是返回结果：

```json
{
  "data": {
    "data": [
      [
        8,
        9
      ],
      [
        9,
        10
      ],
      [
        10,
        10
      ]
    ],
    "schema": [
      {
        "fieldName": "id",
        "visible": true,
        "type": "number"
      },
      {
        "fieldName": "month",
        "visible": true,
        "type": "number"
      }
    ]
  }
}
```

* 示例2： 写 HE 结构体

```json
{
  "kind": "function",
  "op": "select_fields_complete",
  "args": [
    {
      "kind": "function",
      "op": "dataset",
      "args": [
        {
          "kind": "constant",
          "op": 2
        }
      ]
    },
    [
      {
        "kind": "field",
        "op": "id"
      },
      {
        "kind": "field",
        "op": "month"
      }
    ],
    [
      {
        "kind": "function",
        "op": ">",
        "args": [
          {
            "kind": "field",
            "op": "id"
          },
          {
            "kind": "constant",
            "op": 1
          }
        ]
      },
      {
        "kind": "function",
        "op": "<",
        "args": [
          {
            "kind": "field",
            "op": "id"
          },
          {
            "kind": "constant",
            "op": 50
          }
        ]
      }
    ],
    [
      {
        "kind": "field",
        "op": "id",
        "direction": "desc"
      }
    ],
    {
      "kind": "constant",
      "op": 6
    },
    {
      "kind": "constant",
      "op": 3
    }
  ]
}
```

下面是返回结果：

```json
{
  "data": {
    "data": [
      [
        43,
        1
      ],
      [
        42,
        5
      ],
      [
        41,
        10
      ]
    ],
    "schema": [
      {
        "fieldName": "id",
        "visible": true,
        "type": "number"
      },
      {
        "fieldName": "month",
        "visible": true,
        "type": "number"
      }
    ]
  }
}
```

## summarize

此函数返回数据集中选定的列的数据。不同列的运算是相互影响的。根据非聚合列分组后，再计算聚合列。

* 函数语法

```text
summarize({dataset expression}, {field1}, {field2} ...)
```

* 参数说明

  * dataset expression:  必填，返回结果是 dataset 的表达式。
  * field1, field2 ...:  必填，参数数量可变，最少为1，描述列的表达式。

* 示例1

```text
summarize(app_dataset(1, 1), {type})
```

下面是返回结果：

```json
{
  "data": {
    "data": [
      [
        "喜剧"
      ],
      [
        "动画"
      ]
    ],
    "schema": [
      {
        "fieldName": "type",
        "visible": true,
        "nativeType": "text"
      }
    ]
  }
}
```

* 示例2

```text
summarize(app_dataset(1, 1), {type}, sum({votes}) as {sum1}, max({votes}) as {max1})
```

下面是返回结果：

```json
{
  "data": {
    "data": [
      [
        "喜剧",
        16.5,
        8.3
      ],
      [
        "动画",
        16.6,
        8.5
      ]
    ],
    "schema": [
      {
        "fieldName": "type",
        "visible": true,
        "nativeType": "text"
      },
      {
        "fieldName": "sum1",
        "visible": true,
        "nativeType": "numeric"
      },
      {
        "fieldName": "max1",
        "visible": true,
        "nativeType": "numeric"
      }
    ]
  }
}
```

## summarize_complete

此函数返回数据集中选定的列的数据。不同列的运算是相互影响的。根据非聚合列分组后，再计算聚合列。同 SUMMARIZE 不同的是，它可以加入过滤条件、排序、分页信息。

* 函数语法

```text
summarize_complete({dataset expression},
[expr_1,expr_2 ...],
[filter_expr_1,filter_expr_2 ...],
[having_expr_1,having_expr_2 ...],
[sort_expr_1,sort_expr_2...],
offset, limit)
```

* 参数说明

  * dataset expression：必填，返回结果是 dataset 的表达式。
  * \[expr_1,expr_2 ...]：必填，数组长度没有限制，数组为空的时候表示获取所有字段的信息。表达式中所有非聚合字段都会参与到分组计算中。
  * \[filter_expr_1,filter_expr_2 ...]：必填，过滤表达式，不同表达式之间是 and 的关系，为空表示没有过滤条件。
  * \[having_expr_1,having_expr_2 ...]：必填，过滤表达式，不同表达式之间是 and 的关系，为空表示没有过滤条件。这里是聚合表达式的过滤，对应 sql 中的 having。
  * \[sort_expr_1,sort_expr_2...]：必填，排序字段，写表达式的场景下，这里不支持指定顺序，都是按照升级排序。但是写 HE 结构体的时候，是可以指定顺序的。
  * offset：必填，获取数据的偏移量。
  * limit：必填，或者数据的限制条数。

* 示例1: 写表达式

```text
summarize_complete(dataset(2),[{month},count({id})],[{id}>1,{id}<50],[avg({day})>1],[{month}],6,3)
```

下面是返回结果：

```json
{
  "data": {
    "data": [
      [
        8,
        2
      ],
      [
        9,
        2
      ],
      [
        10,
        4
      ]
    ],
    "schema": [
      {
        "fieldName": "month",
        "visible": true,
        "type": "number"
      },
      {
        "fieldName": "_hs_uid_0",
        "visible": true,
        "type": "number"
      }
    ]
  }
}
```

* 示例2： 写 HE 结构体

```json
{
  "kind": "function",
  "op": "summarize_complete",
  "args": [
    {
      "kind": "function",
      "op": "dataset",
      "args": [
        {
          "kind": "constant",
          "op": 2
        }
      ]
    },
    [
      {
        "kind": "field",
        "op": "month",
        "uid": "month-t1"
      },
      {
        "kind": "function",
        "op": "count",
        "args": [
          {
            "kind": "field",
            "op": "id"
          }
        ]
      }
    ],
    [
      {
        "kind": "function",
        "op": ">",
        "args": [
          {
            "kind": "field",
            "op": "id"
          },
          {
            "kind": "constant",
            "op": 1
          }
        ]
      },
      {
        "kind": "function",
        "op": "<",
        "args": [
          {
            "kind": "field",
            "op": "id"
          },
          {
            "kind": "constant",
            "op": 50
          }
        ]
      }
    ],
    [
      {
        "kind": "function",
        "op": "!=",
        "args": [
          {
            "kind": "reference",
            "op": "month-t1"
          },
          {
            "kind": "constant",
            "op": 6
          }
        ]
      }
    ],
    [
      {
        "kind": "field",
        "op": "month",
        "direction": "desc"
      }
    ],
    {
      "kind": "constant",
      "op": 6
    },
    {
      "kind": "constant",
      "op": 3
    }
  ]
}
```

下面是返回结果：

```json
{
  "data": {
    "data": [
      [
        5,
        3
      ],
      [
        4,
        5
      ],
      [
        3,
        5
      ]
    ],
    "schema": [
      {
        "fieldName": "month-t1",
        "visible": true,
        "type": "number"
      },
      {
        "fieldName": "_hs_uid_0",
        "visible": true,
        "type": "number"
      }
    ]
  }
}
```
