การ Pivot ข้อมูล

ชนิดข้อมูลและฟังก์ชันใน 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
ชนิดข้อมูลและฟังก์ชันใน Snowflake

ตาราง "Pivoted"




         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



ชนิดข้อมูลและฟังก์ชันใน Snowflake

การสร้างตาราง Pivoted

SELECT
    *
FROM SCHEMA.TABLE

PIVOT(
-- Aggregation function SUM(<1>)
-- Specify rows to pivot to columns FOR <2> IN (ANY ORDER BY <2>) -- No need to GROUP BY! );

PIVOT เป็นอีกวิธีในการแสดงข้อมูลที่รวมแล้ว โดย "pivot" ค่าให้กลายเป็นคอลัมน์

  • SELECT * ใช้ร่วมกับ EXCLUDE ได้
  • PIVOT ระบุหลัง FROM ...
  • ค่า ANY ใน <2>

$$

<1>: ฟิลด์ที่ต้องการรวมค่า

<2>: ค่าในแถวที่ต้องการแปลงเป็นคอลัมน์

ชนิดข้อมูลและฟังก์ชันใน Snowflake

CTE และข้อมูล Pivoted

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) );

กำหนด CTE ก่อนใช้งาน PIVOT!

$$

             SELECT * FROM <cte>

$$

$$

$$

exam_score: ฟิลด์ที่ต้องการรวมค่า

exam_type: ค่าในแถวที่ต้องการแปลงเป็นคอลัมน์

ชนิดข้อมูลและฟังก์ชันใน Snowflake

เปรียบเทียบคะแนนสอบตามประเภท

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_grades
PIVOT( AVG(exam_score) FOR exam_type IN (ANY ORDER BY exam_type) );
ชนิดข้อมูลและฟังก์ชันใน Snowflake

เปรียบเทียบคะแนนสอบตามประเภท

                 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
ชนิดข้อมูลและฟังก์ชันใน Snowflake

มาฝึกกันเถอะ!

ชนิดข้อมูลและฟังก์ชันใน Snowflake

Preparing Video For Download...