Snowflake에서 데이터 조작
Jake Roach
Field Data Engineer
서브쿼리는 한 쿼리의 결과를 다른 쿼리에서 사용할 수 있게 하는 도구입니다.
$$
$$
FROM ( ... ) 또는 WHERE ... IN ( ... )

SELECT
...
-- Pull from query, not from a table
FROM (
-- Create a result set that will
-- be used by the main query
SELECT
<fields>
FROM <table>
WHERE ...
);
테이블이 아닌 다른 쿼리의 결과에서 데이터를 가져옵니다.
$$
JOIN, WHERE 등에서 사용SELECT
month_num,
-- windchill - temperature has to be used twice here. What if this changes?
AVG(windchill - temperature) AS avg_differential
MIN(windchill - temperature) AS most_differential
FROM weather
WHERE
-- Filtering is taking place in the same query as aggregation/analysis
season = 'Winter' AND
temperature < 32
GROUP BY month_num;
-- Start with the subquery, then aggregateSELECT 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;
| 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 |
데이터를 정리한 뒤에는 분석을 이해하고 변경하기 쉽습니다.
...
-- Filter by records with a value in
-- the subquery result set
WHERE <field> IN (
SELECT <other-field> FROM ...
);
변환, 필터링, 조작에 쓸 작은 결과 집합을 만듭니다.
$$
IN으로 필터링AVG, MAX, MIN 등SELECT
todays_date,
temperature,
status
FROM weather
WHERE todays_date IN ( -- Filter by all days with home games that were won
SELECT
game_date
FROM game_schedule
WHERE stadium = 'Home' AND did_win = TRUE
);

Snowflake에서 데이터 조작