Snowflake에서 데이터 조작
Jake Roach
Field Data Engineer
공통 테이블 식(CTE)은 쿼리 결과를 일시 저장하여 다른 쿼리에서 사용합니다
$$
WITH <cte-name> AS ( <query> )-- CTE 이름을 지정하고 -- 괄호 안에 쿼리를 작성합니다 WITH <cte-name> AS ( <query> )SELECT ... FROM <cte-name> ... ;
WITH at_risk AS ( SELECT student_id course_name, teacher_name, grade FROM student_courses WHERE grade < 70 AND is_required )SELECT students.student_name, at_risk.* FROM at_risk JOIN students ON at_risk.student_id = students.id;
$$
at_risk에 저장됨at_risk를 조회하여 보고서 생성$$
$$
$$

SELECT
month_num,
AVG(differential) AS avg_differential
MIN(differential) AS most_differential
FROM (
SELECT
month_num,
windchill - temperature AS differential
FROM weather
WHERE
season = 'Winter' AND
temperature < 32
)
GROUP BY month_num;
WITH daily_temperature_differential AS (
SELECT
month_num,
windchill - temperature AS differential
FROM weather
WHERE
season = 'Winter' AND
temperature < 32
)
SELECT
month_num,
AVG(differential) AS avg_differential
MIN(differential) AS most_differential
FROM daily_temperature_differential
GROUP BY month_num;
WITH daily_temperature_differential AS ( SELECT month_num, windchill - temperature AS differential FROM weather WHERE season = 'Winter' AND temperature < 32 )SELECT month_num, AVG(differential) AS avg_differential MIN(differential) AS most_differential FROM daily_temperature_differential GROUP BY month_num;
| month_num | differential |
| --------- | ------------ |
| 12 | -12 |
| 1 | -3 |
| 1 | 0 |
| 2 | -7 |
흐름이 서브쿼리보다 더 자연스럽습니다.
| month_num | avg_differential | most_differential |
| --------- | ---------------- | ----------------- |
| 12 | -5.77 | -14 |
| 1 | -1.91 | -8 |
| 2 | -8.13 | -22 |
Snowflake에서 데이터 조작