bigquery with문 활용하기

Updated:

less than 1 minute read

sql에서 복잡한 쿼리를 짜다보면 비슷한 서브쿼리를 연속으로 만들 때가 종종 있습니다. 서브쿼리를 하나의 변수에 담아 사용하듯이 with절로 가상 테이블을 만들어 사용할 수 있습니다.

with df as (
	select col1, col2
  from table1
  where col1 > 10
)
select d.col1, d.col2
from df as d

변수선언과 비슷하고, view를 만들어놓고 호출하는 것과도 비슷합니다.

with df1 as (
	select col1, col2
  from table1
  where col1 > 10
), df2 as(
	select col1, col3
  from table2
)
select df1.col1, df1.col2
from df1
	left join df2 on df1.col1 = df2.col1

위 처럼 복수개의 with문을 활용할 수도 있습니다.

Categories:

Updated: