Tipuri de date și funcții în 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(-- Aggregation function SUM(<1>)-- Specify rows to pivot to columns FOR <2> IN (ANY ORDER BY <2>) -- No need to GROUP BY! );
PIVOT oferă o modalitate diferită de a afișa date agregate prin „pivotarea" valorilor în coloane
SELECT *, se poate folosi EXCLUDEPIVOT urmează după FROM ...ANY în <2>$$
<1>: câmpul de agregat
<2>: valorile din rânduri care devin coloane
WITH exam_grades AS ( SELECT ..., exam_score, exam_type FROM SCHEMA.TABLE WHERE ... )SELECT * -- Could also use EXCLUDE FROM exam_grades PIVOT( AVG(exam_score) FOR exam_type IN (ANY ORDER BY exam_type) );
Mai întâi, definiți un CTE înainte de a utiliza PIVOT!
$$
SELECT * FROM <cte>
$$
$$
$$
exam_score: câmpul de agregat
exam_type: valorile din rânduri care devin coloane
WITH exam_grades AS ( SELECT course_name, course_abbreviation, exam_score, exam_type FROM STUDENTS.grades WHERE course_level = '101' )SELECT * EXCLUDE course_abbreviation -- Remove course_abbrevation from the result set 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
Tipuri de date și funcții în Snowflake