Datentypen und Funktionen 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(-- Aggregationsfunktion SUM(<1>)-- Zeilen zu Spalten pivotieren FOR <2> IN (ANY ORDER BY <2>) -- Kein GROUP BY nötig! );
PIVOT bietet eine andere Ausgabe für aggregierte Daten, indem Werte zu Spalten „gepivotet“ werden
SELECT * kann EXCLUDE nutzenPIVOT steht nach FROM ...ANY Werte in <2>$$
<1>: zu aggregierendes Feld
<2>: Zeilenwerte, die zu Spalten werden
WITH exam_grades AS ( SELECT ..., exam_score, exam_type FROM SCHEMA.TABLE WHERE ... )SELECT * -- Man kann auch EXCLUDE nutzen FROM exam_grades PIVOT( AVG(exam_score) FOR exam_type IN (ANY ORDER BY exam_type) );
Definiere zuerst eine CTE, bevor du PIVOT verwendest!
$$
`SELECT * FROM <cte>`
$$
$$
$$
exam_score: zu aggregierendes Feld
exam_type: Zeilenwerte werden zu Spalten
WITH exam_grades AS ( SELECT course_name, course_abbreviation, exam_score, exam_type FROM STUDENTS.grades WHERE course_level = '101' )SELECT * EXCLUDE course_abbreviation -- Entferne course_abbrevation aus dem Ergebnis 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
Datentypen und Funktionen in Snowflake