Snowflake 中的数据类型与函数
Jake Roach
Field Data Engineer

比较运算符用于比较或评估多个值
=, 两值是否相等?!=, 两值是否不相等?<, 是否小于另一个?>, 是否大于另一个?<= 小于或等于>=, 大于或等于$$
返回 `true` 或 `false`!
$$
1 = 1, -- true1 != 1, -- false1 < 2, -- true1 > 2, -- false1 <= 2, -- true2 >= 2 -- true... WHERE 1 = 1 -- 过滤记录
SELECT
<#> + <#>, -- 2 + 2 -> 4
<field> - <#>, -- 4 - 1 -> 3
<field> * <#>, -- 3 * 2 -> 6
<field> / <#>, -- 9 / 3 -> 3
...
# 与 field 间任意组合计算算术运算符用于对数值进行"运算"
$$
+ 加法- 减法* 乘法/ 除法SELECT student_name, exam_score,exam_score + 10 AS add_points, -- 每位学生加 10 分exam_score * curve AS curved, -- 按 10% 曲线加分exam_score / 2 AS weighted -- 降低该考试权重FROM STUDENTS.grades;
student_name | exam_score | add_points | curved | weighted
-------------- | -------------- | ------------ | -------- | ----------
Ryan | 78 | 88 | 85.8 | 39
Tatiana | 89 | 99 | 97.9 | 44.5
Pankaj | 74 | 84 | 81.4 | 37

SELECT<1>,SUM(<field>), -- 返回列的总和AVG(<field>) -- 求列的平均值FROM ...GROUP BY <1>;
必须对非聚合字段使用 GROUP BY!
GROUP BY ALLSELECT exam_name, SUM(correct_answers) AS total_correct_answers, -- 正确数总计 AVG(exam_score) AS avg_exam_score, -- 平均分ROUND(AVG(exam_score), 1) AS rounded_exam_score -- ROUND(<value>, <n>)FROM STUDENTS.grades GROUP BY exam_name; -- 需用 GROUP BY 聚合记录,否则报错
ROUND() 接受要四舍五入的值和小数位数
exam_name | total_correct_answers | avg_exam_score | rounded_avg_exam_score
------------- | ----------------------- | ---------------- | ------------------------
Calculus I | 871 | 89.11111 | 89.1
Biology | 776 | 87.47777 | 87.5
English III | 541 | 91.33333 | 91.3
Python | 1179 | 92.78787 | 92.8
Finance | 349 | 96.14156 | 96.1
这些值由以下生成:
SUM(correct_answers)AVG(exam_score)ROUND(AVG(exam_score), 2)Snowflake 中的数据类型与函数