Gegevenstypen en functies in Snowflake
Jake Roach
Field Data Engineer
course_name | exam_type | avg_exam_score
------------- | ----------- | ----------------
Calculus I | Exam 1 | 81.78
Calculus I | Exam 2 | 83.55
Calculus I | Final | 80.93
Finance | Exam 1 | 89.47
Finance | Exam 2 | 90.39
Finance | Final | 89.69
Marketing | Exam 1 | 94.11
Marketing | Exam 2 | 93.29
Marketing | Final | 93.81
course_name | "Exam 1" | "Exam 2" | "Final"
------------- | ------------- | ------------- | ---------
Calculus I | 81.78 | 83.55 | 80.93
Finance | 89.47 | 90.39 | 89.69
Marketing | 94.11 | 93.29 | 93.81
SELECT * FROM SCHEMA.TABLEPIVOT(-- Aggregatiefunctie SUM(<1>)-- Rijen naar kolommen draaien FOR <2> IN (ANY ORDER BY <2>) -- Geen GROUP BY nodig! );
PIVOT geeft geaggregeerde data weer door waarden naar kolommen te “pivoteren”
SELECT * kan met EXCLUDEPIVOT komt na FROM ...ANY waarden in <2>$$
<1>: veld om te aggregeren
<2>: rijwaarden die kolommen worden
WITH exam_grades AS ( SELECT ..., exam_score, exam_type FROM SCHEMA.TABLE WHERE ... )SELECT * -- Je kunt ook EXCLUDE gebruiken FROM exam_grades PIVOT( AVG(exam_score) FOR exam_type IN (ANY ORDER BY exam_type) );
Definieer eerst een CTE vóór je PIVOT gebruikt!
$$
SELECT * FROM <cte>
$$
$$
$$
exam_score: te aggregeren veld
exam_type: rijwaarden die kolommen worden
WITH exam_grades AS ( SELECT course_name, course_abbreviation, exam_score, exam_type FROM STUDENTS.grades WHERE course_level = '101' )SELECT * EXCLUDE course_abbreviation -- Verwijder course_abbrevation uit de resultaten FROM exam_gradesPIVOT( AVG(exam_score) FOR exam_type IN (ANY ORDER BY exam_type) );
course_name | exam_type | avg_exam_score
------------- | ------------- | ----------------
Calculus I | Exam 1 | 81.78
Calculus I | Exam 2 | 83.55
Calculus I | Final | 80.93
Finance | Midterm 1 | 89.47
...
course_name | "Exam 1 " | "Exam 2" | "Final"
------------- | ------------- | ------------- | ---------
Calculus I | 81.78 | 83.55 | 80.93
Finance | 89.47 | 90.39 | 89.69
Marketing | 94.11 | 93.29 | 93.81
Gegevenstypen en functies in Snowflake