Types de données et fonctions dans 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(-- Fonction d'agrégation SUM(<1>)-- Spécifier les lignes à convertir en colonnes FOR <2> IN (ANY ORDER BY <2>) -- Pas besoin de GROUP BY ! );
PIVOT propose un autre format d'agrégation en « pivotant » des valeurs en colonnes
SELECT *, possible d'utiliser EXCLUDEPIVOT vient après FROM ...ANY dans <2>$$
<1> : champ à agréger
<2> : valeurs de lignes à transformer en colonnes
WITH exam_grades AS ( SELECT ..., exam_score, exam_type FROM SCHEMA.TABLE WHERE ... )SELECT * -- On peut aussi utiliser EXCLUDE FROM exam_grades PIVOT( AVG(exam_score) FOR exam_type IN (ANY ORDER BY exam_type) );
D'abord, définissez une CTE avant d'utiliser PIVOT !
$$
SELECT * FROM <cte>
$$
$$
$$
exam_score : champ à agréger
exam_type : valeurs de lignes à transformer en colonnes
WITH exam_grades AS ( SELECT course_name, course_abbreviation, exam_score, exam_type FROM STUDENTS.grades WHERE course_level = '101' )SELECT * EXCLUDE course_abbreviation -- Supprimer course_abbrevation du résultat 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
Types de données et fonctions dans Snowflake