HiveSQL(Hive SQL)是Apache Hive所使用的SQL方言,专门用于在Hadoop上进行大规模数据处理。以下是一些常见的HiveSQL函数及其使用方法:
统计记录数。
SELECT COUNT(*) FROM employees;
计算数值列的总和。
SELECT SUM(salary) FROM employees;
计算数值列的平均值。
SELECT AVG(salary) FROM employees;
返回数值列的最大值。
SELECT MAX(salary) FROM employees;
返回数值列的最小值。
SELECT MIN(salary) FROM employees;
连接两个或多个字符串。
SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM employees;
截取字符串的子串。
SELECT SUBSTR(first_name, 1, 3) AS short_name FROM employees;
返回字符串的长度。
SELECT LENGTH(first_name) AS name_length FROM employees;
将字符串转换为大写。
SELECT UPPER(first_name) AS upper_name FROM employees;
将字符串转换为小写。
SELECT LOWER(first_name) AS lower_name FROM employees;
返回数值的绝对值。
SELECT ABS(salary) AS abs_salary FROM employees;
返回不小于指定数值的最小整数。
SELECT CEIL(salary) AS ceil_salary FROM employees;
返回不大于指定数值的最大整数。
SELECT FLOOR(salary) AS floor_salary FROM employees;
将数值四舍五入到指定的小数位数。
SELECT ROUND(salary, 2) AS rounded_salary FROM employees;
返回当前日期。
SELECT CURRENT_DATE AS today FROM employees;
返回当前时间戳。
SELECT CURRENT_TIMESTAMP AS now FROM employees;
计算两个日期之间的天数差。
SELECT DATEDIFF('2024-12-31', '2024-01-01') AS days_diff FROM employees;
在指定日期上加上指定的天数。
SELECT DATE_ADD('2024-01-01', 10) AS new_date FROM employees;
在指定日期上减去指定的天数。
SELECT DATE_SUB('2024-01-01', 10) AS new_date FROM employees;
分别提取日期的年份、月份和天。
SELECT YEAR(hire_date) AS hire_year, MONTH(hire_date) AS hire_month, DAY(hire_date) AS hire_day FROM employees;
根据条件返回不同的值。
SELECT IF(salary > 5000, 'High', 'Low') AS salary_level FROM employees;
多条件判断,类似于SQL中的CASE WHEN。
SELECT
CASE
WHEN salary > 7000 THEN 'Very High'
WHEN salary > 5000 THEN 'High'
ELSE 'Low'
END AS salary_level
FROM employees;
返回数组或映射的大小。
SELECT SIZE(skill_set) AS skill_count FROM employees;
返回映射的所有键。
SELECT MAP_KEYS(employee_info) AS keys FROM employees;
返回映射的所有值。
SELECT MAP_VALUES(employee_info) AS values FROM employees;
检查数组中是否包含指定元素。
SELECT ARRAY_CONTAINS(skill_set, 'Java') AS has_java_skill FROM employees;
从JSON字符串中提取指定的字段。
SELECT GET_JSON_OBJECT(employee_json, '$.name') AS employee_name FROM employees;
将JSON字符串解析为多个列。
SELECT
JSON_TUPLE(employee_json, 'name', 'age', 'department')
FROM employees;
为结果集中的每一行分配唯一的行号。
SELECT
employee_id,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS row_num
FROM employees;
为结果集中的每一行分配排名,排名相等的行具有相同的排名。
SELECT
employee_id,
RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS rank
FROM employees;
为结果集中的每一行分配密集排名,排名相等的行具有相同的排名,但没有空缺排名。
SELECT
employee_id,
DENSE_RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS dense_rank
FROM employees;
返回第一个非NULL的值。
SELECT COALESCE(middle_name, first_name, 'N/A') AS name FROM employees;
如果第一个参数为NULL,则返回第二个参数,否则返回第一个参数。
SELECT NVL(middle_name, 'N/A') AS middle_name FROM employees;
使用正则表达式从字符串中提取子字符串。
SELECT REGEXP_EXTRACT(email, '([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+)', 1) AS email_address FROM employees;
将字符串按指定分隔符分割成数组。
SELECT SPLIT(full_name, ' ') AS name_parts FROM employees;