ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [SQL] data.world tutorial SQL Inline VALUE 문 예제 풀이
    데이터 분석/DB & SQL 2020. 5. 3. 23:03

    Value Block

    VLOOKUP 처럼 활용할 때 사용한다.
    매칭해야 하는 값이 몇개 없을 때에는 테이블로 따로 빼지 않고 이렇게 쿼리 안에 정의해서 활용할 수도 있다. 

     

    A value block is a table created and defined within a query that exists only in the scope of that query. It acts like any other table and can be used to extend the data in an existing table by means of a small look up.

    value 블럭은 쿼리 내부에 생성 및 정의되는 테이블이며, 그 쿼리가 실행될 때에만 존재한다. 다른 테이블처럼 동작하고, 기존 테이블에 있는 데이터에 간단한 lookup 을 처리하는데 사용된다.

     


    1. 요구사항 검토

    Let’s say we wanted to add the city locations of the regional offices for each sales person to the results of a query.

    결과 데이터에 regional office 값에 매칭되는 city 를 추가하고 싶다. 

     

    2. 데이터셋 확인

    While the regions are stored in our tables, the cities where they are headquartered are not.

    regional_office 는 있지만  (headquartered) city 값이 저장되어있지 않다. (즉, 매칭값이 필요하다.)

    sales_teams: manager, sales_agent, regional_office (East, West, Center)
    value block: regional_office, city

     

    3. 쿼리 구조

    Value Block 은 테이블처럼 사용해주면 된다. sales_teams 와 Value Block 을 JOIN 해서 결과셋을 제공할 수 있다.

    SELECT ( sales_teams 의 컬럼들과 city )
    FROM ( VALUE ( sales_teams.regional_office 와 city 값 매칭))
    JOIN sales_teams  ON ( join 조건 ) 

     

    4. 쿼리 작성

    SELECT Regional_headquarters.city,
            sales_teams.sales_agent,
            sales_teams.manager

    FROM
    (
    VALUES  ("Central", "Chicago"), ("East", "New York"), ("West", "San Francisco")
    AS Regional_headquarters(region, city) 

    JOIN sales_teams ON Regional_headquarters.region = sales_teams.regional_office

     


    저런 Value Block 은  소스코드 컨피그에 들어갈 것만 같긴 하다. ;) 

    댓글

Designed by Tistory.