> ## Documentation Index
> Fetch the complete documentation index at: /documents/llms.txt
> Use this file to discover all available pages before exploring further.

# percentile_disc_lc

> percentile_disc_lc 计算百分位数，使用 hash 表实现，低基数输入时内存占用少。

计算百分位数。和 percentile\_disc 行为一致. 但是实现算法不同 percentile\_disc 需要获取所有的输入数据，通过归并排序获取百分位的值需要消耗的内存为所有输入数据的内存。而 percentile\_disc\_lc 是构建一个 key->count的一个hash表，因此当输入的基数比较低的时候即使输入的数据量很大也没有明显的内存增长。

该函数从 3.4 版本开始支持。

## 语法

```SQL theme={null}
PERCENTILE_DISC_LC (expr, percentile) 
```

## 参数说明

* `expr`: 要计算百分位数的列，列值支持任意可排序的类型。

* `percentile`: 指定的百分位，介于 0 和 1 之间的浮点常量。如果要计算中位数，则设置为 0.5。

## 返回值说明

返回指定的百分位对应的值。如果没有找到与百分位完全匹配的值，则返回临近两个数值中较大的值。

返回值类型与 `expr` 内的数据类型相同。

## 使用说明

该函数忽略 Null 值。

## 示例

创建表 `exam`，插入数据。

```sql theme={null}
CREATE TABLE exam (
    subject STRING,
    score INT
) 
DISTRIBUTED BY HASH(`subject`);

INSERT INTO exam VALUES
('chemistry',80),
('chemistry',100),
('chemistry',null),
('math',60),
('math',70),
('math',85),
('physics',75),
('physics',80),
('physics',85),
('physics',99);
```

```Plain theme={null}
select * from exam order by subject;
+-----------+-------+
| subject   | score |
+-----------+-------+
| chemistry |    80 |
| chemistry |   100 |
| chemistry |  NULL |
| math      |    60 |
| math      |    70 |
| math      |    85 |
| physics   |    75 |
| physics   |    80 |
| physics   |    85 |
| physics   |    99 |
+-----------+-------+
```

计算每个科目得分的中位数，忽略 Null 值。

查询语句：

```SQL theme={null}
select subject, percentile_disc_lc (score, 0.5)
from exam group by subject;
```

返回结果：

```Plain theme={null}
+-----------+--------------------------------+
| subject   | percentile_disc_lc(score, 0.5) |
+-----------+--------------------------------+
| physics   |                             85 |
| chemistry |                            100 |
| math      |                             70 |
+-----------+--------------------------------+
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.