数值函数

rand

rand() 返回一个范围在 0(包含)到 1(不包含)之间的随机浮点数,即 [0,1)。返回的数字遵循近似均匀分布。

语法:rand()

返回值:

浮点数。

查询:

SELECT *
FROM cypher('graph_name', $$
    RETURN rand()
$$) as (random_number agtype);

返回一个随机数。

结果:

    random_number
---------------------
 0.22059738168426435
(1 row)

abs

abs() 返回给定数字的绝对值。

语法:abs(expression)

返回值:

返回值的类型与 expression 的类型相同。

参数:

名称 描述
expression agtype 数值表达式

注意事项:

  • abs(null) 返回 null。
  • 如果 expression 为负数,则返回 -(expression)(即 expression 的相反数)。

查询:

SELECT *
FROM cypher('graph_name', $$
    MATCH (a), (e) WHERE a.name = 'Alice' AND e.name = 'Eskil'
    RETURN a.age, e.age, abs(a.age - e.age)
$$) as (alice_age agtype, eskil_age agtype, difference agtype);

返回年龄差的绝对值。

结果:

 alice_age | eskil_age | difference
-----------+-----------+------------
 38        | 41        | 3
(1 row)

ceil

ceil() 返回大于或等于给定数字且等于某个数学整数的最小浮点数。

语法:ceil(expression)

返回值:

浮点数。

参数:

名称 描述
expression agtype 数值表达式

注意事项:

  • ceil(null) 返回 null

查询:

SELECT *
FROM cypher('graph_name', $$
    RETURN ceil(0.1)
$$) as (ceil_value agtype);

返回 0.1 的向上取整值。

结果:

 ceil_value
------------
 1.0
(1 row)

floor

floor() 返回小于或等于给定数字且等于某个数学整数的最大浮点数。

语法:floor(expression)

返回值:

浮点数。

参数:

名称 描述
expression agtype 数值表达式

注意事项:

  • floor(null) 返回 null。

查询:

SELECT *
FROM cypher('graph_name', $$
    RETURN floor(0.1)
$$) as (flr agtype);

返回 0.1 的向下取整值。

结果:

 flr
-----
 0.0
(1 row)

round

round() 返回给定数字四舍五入到最接近整数的值。

语法:round(expression)

返回值:

浮点数。

参数:

名称 描述
expression agtype 数值表达式

注意事项:

  • round(null) 返回 null

查询:

SELECT *
FROM cypher('graph_name', $$
    RETURN round(3.141592)
$$) as (rounded_value agtype);

返回 3.0。

结果:

 rounded_value
---------------
 3.0
(1 row)

sign

sign() 返回给定数字的符号值:如果数字为 0 则返回 0,任何负数返回 -1,任何正数返回 1。

语法:sign(expression)

返回值:

整数。

参数:

名称 描述
expression agtype 数值表达式

注意事项:

  • sign(null) 返回 null

查询:

SELECT *
FROM cypher('graph_name', $$
    RETURN sign(-17), sign(0.1), sign(0)
$$) as (negative_sign agtype, positive_sign agtype, zero_sign agtype);

返回 -17 和 0.1 的符号值。

结果:

 negative_sign | positive_sign | zero_sign
---------------+---------------+-----------
 -1            | 1             | 0
(1 row)