การคำนวณเชิงตัวเลข

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

Jake Roach

Field Data Engineer

การคำนวณเชิงตัวเลข

การคำนวณและการจัดการเชิงตัวเลขสามประเภท

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

การเปรียบเทียบค่าเชิงตัวเลข

ตัวดำเนินการเปรียบเทียบช่วยเปรียบเทียบหรือประเมินค่าหลายค่า

  • =, ค่าทั้งสองเท่ากันหรือไม่?
  • !=, ค่าทั้งสองไม่เท่ากันหรือไม่?
  • <, ค่าหนึ่งน้อยกว่าอีกค่าหรือไม่?
  • >, ค่าหนึ่งมากกว่าอีกค่าหรือไม่?
  • <= น้อยกว่าหรือเท่ากับ
  • >=, มากกว่าหรือเท่ากับ

$$

            คืนค่า true หรือ false!

$$

        1 = 1,   -- true

1 != 1, -- false
1 < 2, -- true
1 > 2, -- false
1 <= 2, -- true
2 >= 2 -- true
... WHERE 1 = 1 -- Filter records
ชนิดข้อมูลและฟังก์ชันใน Snowflake

การคำนวณเลขคณิตใน Snowflake

SELECT

    <#> + <#>,        -- 2 + 2  ->  4
    <field> - <#>,    -- 4 - 1  ->  3
    <field> * <#>,    -- 3 * 2  ->  6
    <field> / <#>,    -- 9 / 3  ->  3

                ...
  • ใช้ได้กับการผสมระหว่าง # และ field ในรูปแบบต่างๆ

ตัวดำเนินการเลขคณิตช่วยให้คำนวณทางคณิตศาสตร์กับค่าเชิงตัวเลขได้

$$

  • +, การบวก
  • -, การลบ
  • *, การคูณ
  • /, การหาร
ชนิดข้อมูลและฟังก์ชันใน Snowflake

เลขคณิต

SELECT
    student_name, exam_score,

exam_score + 10 AS add_points, -- Add 10 points to each student's grade
exam_score * curve AS curved, -- Curve the grade by 10%
exam_score / 2 AS weighted -- Reduce the weight of the test
FROM STUDENTS.grades;
              student_name  |   exam_score   |  add_points  |  curved  |  weighted  
             -------------- | -------------- | ------------ | -------- | ----------
               Ryan         |       78       |      88      |   85.8   |     39
               Tatiana      |       89       |      99      |   97.9   |    44.5
               Pankaj       |       74       |      84      |   81.4   |     37
ชนิดข้อมูลและฟังก์ชันใน Snowflake

ฟังก์ชัน Aggregation สร้างข้อมูลสรุป

ภาพประกอบการรวมกลุ่มระเบียนด้วย GROUP BY

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

ฟังก์ชัน Aggregation

 SELECT

<1>,
SUM(<field>), -- Returns the total of a column
AVG(<field>) -- Finds the average value of a column
FROM ...
GROUP BY <1>;

ต้องใช้ GROUP BY กับฟิลด์ที่ไม่ได้ถูก Aggregate!

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

ฟังก์ชัน Aggregation

SELECT
    exam_name,

    SUM(correct_answers) AS total_correct_answers,      -- Total # correct
    AVG(exam_score) AS avg_exam_score,                  -- Average exam score


ROUND(AVG(exam_score), 1) AS rounded_exam_score -- ROUND(<value>, <n>)
FROM STUDENTS.grades GROUP BY exam_name; -- GROUP BY to aggregate records, otherwise error

ROUND() รับค่าที่ต้องการปัดเศษ และจำนวนหลักทศนิยมที่ต้องการเก็บไว้

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

ฟังก์ชัน Aggregation

   exam_name    |  total_correct_answers  |  avg_exam_score  |  rounded_avg_exam_score 
  ------------- | ----------------------- | ---------------- | ------------------------ 
   Calculus I   |           871           |      89.11111    |           89.1     
   Biology      |           776           |      87.47777    |           87.5
   English III  |           541           |      91.33333    |           91.3
   Python       |          1179           |      92.78787    |           92.8
   Finance      |           349           |      96.14156    |           96.1

ค่าเหล่านี้คำนวณโดยใช้:

  • SUM(correct_answers)
  • AVG(exam_score)
  • ROUND(AVG(exam_score), 2)
ชนิดข้อมูลและฟังก์ชันใน Snowflake

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

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

Preparing Video For Download...