Data Manipulation in Snowflake
Jake Roach
Field Data Engineer
$$
Manipulate and transform data based on certain conditions.
Leverage the results of a query in another.
Define a temporary result set for use in a main query.
SELECT
todays_date,
temperature,
status,
CASE
WHEN temperature > 70 AND status NOT IN ('Rain', 'Wind') THEN 'Beach'
WHEN temperature BETWEEN 45 AND 70 AND status = 'Sun' THEN 'Sports'
WHEN temperature <= 32 OR status = 'Snow' THEN 'Skiing'
ELSE 'Stay In'
END AS activity
FROM weather;
Conditional logic can be combined with things like aggregation functions.

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 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;
Common Table Expressions (CTE's) temporarily store the results of a query to eventually be used by another query
$$
Data Manipulation in Snowflake