使用频率较高的数值函数如下:
使用示例:
select ceil(-1.23)
, floor(1.23)
, rand()
, rand(1)
, round(0.123, 2)
, truncate(1.223, 1);
+-----------+-----------+------------------+------------------+---------------+-------------------+
|ceil(-1.23)|floor(1.23)| rand() | rand(1) |round(0.123, 2)|truncate(1.223, 1) |
+-----------+-----------+------------------+------------------+---------------+-------------------+
| -1.0 | 1 |0.4990247756570755|0.7133693869548766| 0.12 | 1.2 |
+-----------+-----------+------------------+------------------+---------------+-------------------+
select length('databend') as len
, char_length('databend') as char_len
, lower('Databend') as lower_char
, upper('Databend') as upper_char
, left('Databend', 4) as left_char
, right('Databend', 4) as right_char;
+-------+----------+------------+------------+-----------+------------+
| len | char_len | lower_char | upper_char | left_char | right_char |
+-------+----------+------------+------------+-----------+------------+
| 8 | 8 | databend | DATABEND | Data | bend |
+-------+----------+------------+------------+-----------+------------+
select trim(leading '?' from '???Databend???') as leading_trim
, trim(trailing '?' from '???Databend???') as trailing_trim
, trim(both '?' from '???Databend???') as both_trim
, trim(' Databend ') as spaces_trim
, repeat('Databend', 2) as repeat_char
, lpad('36363', 10, '0') as lpad_char
, lpad('36363', 10, '?') as rpad_char;
+-------------+-------------+-----------+-------------+------------------+------------+------------+
|leading_trim |trailing_trim| both_trim | spaces_trim | repeat_char | lpad_char | rpad_char |
+-------------+-------------+-----------+-------------+------------------+------------+------------+
| Databend??? | ???Databend | Databend | Databend | DatabendDatabend | 0000036363 | ?????36363 |
+-------------+-------------+-----------+-------------+------------------+------------+------------+
select locate(' ', 'Databend Clound') as pos_char
, substring('Databend Clound', locate(' ', 'Databend Clound')) as sub_char
, insert('Databend Clound', length('Databend Clound'), 7, ' Server') as insert_char
, replace('Databend Clound', 'Databend', 'Mysql') as replace_char1
, replace('Databend Clound', 'Databend', '') as replace_char2
, replace('www.mysql.com', 'mysql', '') as replace_char3;
+----------+----------+-----------------------+---------------+---------------+---------------+
| pos_char | sub_char | insert_char | replace_char1 | replace_char2 | replace_char3 |
+----------+----------+-----------------------+---------------+---------------+---------------+
| 9 | Clound | Databend Cloun Server | Mysqld Clound | d Clound | www.l.com |
+----------+----------+-----------------------+---------------+---------------+---------------+
从最后几列可以看出,Databend 和 Mysql 替换过程中始终不能完整替换,因此在使用过程中,多去测试看结果再实际应用。
另外,Databend 不支持 Mysql 中的 substring_index()函数,但是可以发散思维,比如上面的 sub_char 实现,还有以下方法实现:
select substring('Databend Clound',1,locate(' ','Databend Clound')-1) as sub_char;
+------------+
| sub_char |
+------------+
| Databend |
+------------+
select concat('data', 'bend') as concat_char1
, concat('data', 'bend', null) as concat_char2
, concat('data', 'bend', 1::varchar) as concat_char3
, concat_ws('、', 'data', 'bend') as concat_char4;
+--------------+--------------+---------------+--------------+
| concat_char1 | concat_char2 | concat_char3 | concat_char4 |
+--------------+--------------+---------------+--------------+
| databend | NULL | databend1 | data、bend |
+--------------+--------------+---------------+--------------+
select ifnull(null, 'a') as t1
, greatest(2, 3) as t2
, least(2, 3, 4) as t3
, if(1 > 2, false, true) as t4
, coalesce(null, 'Databend', 'Mysql', null) as t5
;
对于这类函数,主要针对一些特殊的数据类型 variant ,可以前往【Databend】数据类型查看说明。
select parse_json('[-1, 12, 289, 2188, false]') as list_json
, parse_json('{ "x" : "abc", "y" : false, "z": 10} ') as key_json
, object_keys(parse_json('{"a": 1, "b": [1,2,3]}')) as get_keys
, get_path(parse_json('{"k1":[0,1,2], "k2":{"k3":3,"k4":4}}'), 'k1[0]') as k1
, get_path(parse_json('{"k1":[0,1,2], "k2":{"k3":3,"k4":4}}'), 'k2:k3') as k3
, get_path(parse_json('{"k1":[0,1,2], "k2":{"k3":3,"k4":4}}'), 'k2.k4') as k4
, get_path(parse_json('{"k1":[0,1,2], "k2":{"k3":3,"k4":4}}'), 'k2.k5') as k5
, get(parse_json('[2.71, 3.14]'), 0) as get_list_value
, get(parse_json('{"aa":1, "aa":2, "aa":3}'), 'aa') as get_key_values
, get(parse_json('{"aa":1, "aa":2, "aa":3}'), 'aa') as get_null
;
+------------------------+------------------------------+-----------+---+---+---+------+--------------+--------------+--------+
| list_json | key_json | get_keys | k1| k3| k4| k5 |get_list_value|get_key_values|get_null|
+------------------------+------------------------------+-----------+---+---+---+------+--------------+--------------+--------+
| [-1,12,289,2188,false] | {"x":"abc","y":false,"z":10} | ["a","b"] | 0 | 3 | 4 | NULL | 2.71 | 1 | NULL |
+------------------------+------------------------------+-----------+---+---+---+------+--------------+--------------+--------+
with t1 as
(select 'laptop' as name,
'{"brand": "dell", "colors": ["black", "silver"], "price": 1200, "features": {"ram": "16gb", "storage": "512gb"}}' as details
union all
select 'smartphone' as name,
'{"brand": "apple", "colors": ["white", "black"], "price": 999, "features": {"ram": "4gb", "storage": "128gb"}}' as details
union all
select 'headphones' as name,
'{"brand": "sony", "colors": ["black", "blue", "red"], "price": 150, "features": {"battery": "20h", "bluetooth": "5.0"}}' as details)
select name, json_path_query_array(parse_json(details), '$.features.*') as all_features
from t1;
+-----------+---------------------+
| name | all_features |
+-----------+---------------------+
| Laptop | ["16GB", "512GB"] |
+-----------+---------------------+
| Smartphone| ["4GB", "128GB"] |
+-----------+---------------------+
| Headphones| ["20h", "5.0"] |
+-----------+---------------------+
with t1 as
(select 'laptop' as name,
'{"brand": "dell", "colors": ["black", "silver"], "price": 1200, "features": {"ram": "16gb", "storage": "512gb"}}' as details
union all
select 'smartphone' as name,
'{"brand": "apple", "colors": ["white", "black"], "price": 999, "features": {"ram": "4gb", "storage": "128gb"}}' as details
union all
select 'headphones' as name,
'{"brand": "sony", "colors": ["black", "blue", "red"], "price": 150, "features": {"battery": "20h", "bluetooth": "5.0"}}' as details)
select name, json_path_query(parse_json(details), '$.features.*') as all_features
from t1;
+------------+--------------+
| name | all_features |
+------------+--------------+
| Laptop | "16GB" |
| Laptop | "512GB" |
| Smartphone | "4GB" |
| Smartphone | "128GB" |
| Headphones | "20h" |
| Headphones | "5.0" |
+------------+--------------+
主要介绍常用的几种,如下:
-- 数据准备
create table if not exists program_languages (
id int,
language_name varchar,
score int
);
insert into program_languages (id, language_name,score)
values (1, 'python',80),
(2, 'javascript',90),
(3, 'java',75),
(4, 'c#',95),
(5, 'ruby',85);
-- 指标计算
select avg(score) as avg_score
, max(score) as max_score
, min(score) as min_score
, median(score) as median_score
, sum(score) as total_score
, count(distinct language_name) as language_cnt
, string_agg(language_name, '、') as language_concat
from program_languages;
+-----------+-----------+-----------+--------------+-------------+--------------+-----------------------------------+
| avg_score | max_score | min_score | median_score | total_score | language_cnt | language_concat |
+-----------+-----------+-----------+--------------+-------------+--------------+-----------------------------------+
| 85 | 95 | 75 | 85 | 425 | 5 | python、javascript、java、c#、ruby |
+-----------+-----------+-----------+--------------+-------------+--------------+-----------------------------------+
以上这些聚合函数 null 值都不计算在内。
聚合函数 Databend 比 Mysql 多了一个 median() 可用于更好地计算中位数,其实还有一些其他聚合函数,有兴趣的可以自己扩展。
Databend 作为新一代云原生数据仓库,提供了许多多样化的函数,函数与 Mysql 相比,并无较大差异,我们只要掌握基础常用哪些函数,基本上可以解决工作中大部分问题。
参考资料: