ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [SQL] data.world tutorial SQL GROUP BY FILTER, HAVING 예제 풀이
    데이터 분석/DB & SQL 2020. 5. 1. 12:42

    GROUP BY 집계를 더 세련되게 하기 위해서

    https://docs.data.world/documentation/sql/concepts/intermediate/GROUP_BY.html

     

    GROUP BY and FILTER

    Learn about GROUP BY and FILTER in this SQL tutorial. Visit for a full course on learning the basics of SQL.

    docs.data.world

     

    Filter 에 대한 1 줄 정리

    refine your query by running your aggregations against a limited set of the values in a column you could use the FILTER keyword.

    제한된 값에 한해서만 집계를 하고 싶다면 FILTER 키워드를 사용합니다. 

     

    HAVING 에 대한 2 줄 정리

    HAVING is used with aggregations to filter out results returned by the aggregation.  It is similar to WHERE except that WHERE removes values before the aggregation function is applied to the values, and HAVING removes values after aggregation has occurred.

    HAVING 은 집계할 때, 결과 셋에서 일부를 필터링할 때 사용한다. WHERE 와 유사하게 동작하며, WHERE 는 집계 전에 데이터에 대해서 적용하는데 반해, HAVING 은 집계 후에 데이터에 대해 적용한다. 

    FILTER 와 HAVING 은 사용자의 추가 가공 없이 특정 지표에 집중할 수 있게 도와준다. 일반적으로 사용자는 원하는 결과를 자신들이 직접 만져보고 싶어하기 때문에 FILTER, HAVING 키워드를 사용할 기회가 별로 없었지만. 


    1. 요구사항을 검토한다. 

    For example, if you wanted to know both the number of deals won by a sales agent and the number of those deals that had a value greater than 1000, you could use the query:

    - know both the number of deals won by a sales agent: sales_agent 단위로 성사된 거래량
    - the number of those deals that had a value greater than 1000: 그 중 1000 이상의 가치를 가진 거래량

    집계결과 중 제한된 조건을 가진 데이터의 집계결과만 추가적으로 알고 싶다!

    요구사항: close_value > 1000 인 거래건을 100 개 이상 가진 sales_agent 의 데이터만 보고싶다. 

    2. Dataset 을 확인한다.

    이전 글에 있으니까 생략

     

    3. 집계대상 조건부 추출, FILTER

    집계대상을 제한된 조건을 가진 데이터로 좁혀서 집계할 경우 FILTER 키워드를 통해 해결할 수 있다.

    집계결과컬럼 FILTER (WHERE 조건) 

    a. 갯수 COUNT 집계결과

    - total: salesvalue >0 의 전체 갯수와
    - over 1000: salesvalue >1,000 의 갯수 (동일한 대상, 추가 조건)

    SELECT sales_agent,
            COUNT(pipeline.close_value) AS `total`,
            COUNT(pipeline.close_value)
            FILTER(WHERE pipeline.close_value > 1000) AS `over 1000`
    FROM sales_pipeline AS pipeline
    WHERE pipeline.deal_stage = "Won"
    GROUP BY pipeline.sales_agent
    ORDER BY total

     

    b. 평균 AVG 집계결과 추가

    - average of total: salesvalue >0 의 전체 평균과
    - average of over 1000: salesvalue >1,000 의 평균 (동일한 대상, 추가 조건)

    SELECT sales_agent,
            COUNT(pipeline.close_value) AS `total`,
            COUNT(pipeline.close_value)
            FILTER(WHERE pipeline.close_value > 1000) AS `over 1000`,
            AVG(sales_pipeline.close_value) AS `average of total`,
            AVG(sales_pipeline.close_value)
            FILTER(WHERE sales_pipeline.close_value > 1000) AS `average of over 1000`

    FROM sales_pipeline AS pipeline
    WHERE pipeline.deal_stage = "Won"
    GROUP BY pipeline.sales_agent
    ORDER BY total

     

     

    4. 집계결과 조건부 추출, HAVING

    집계결과 데이터셋에서 제한된 조건에 부합하는 결과 row 만 추출하고 싶다면 HAVING 키워드를 통해 해결할 수 있다.

    요구사항: close_value > 1000 인 거래건을 100 개 이상 가진 sales_agent 의 데이터만 보고싶다. 

     

    복잡한 쿼리를 단순하게 Step By Step 좁히는 과정.

    a. sales_agent 데이터: HAVING 절 불필요

     

    b. 거래건을 100 개 이상 가진 sales_agent 의 데이터만 보고 싶다면

    HAVING COUNT(pipeline.close_value) >  100

     

    c. close_value > 1000 인 거래건을 100 개 이상 가진 sales_agent 의 데이터만 보고 싶다면

    HAVING COUNT(sales_pipeline.close_value) FILTER(WHERE sales_pipeline.close_value > 1000) > 100

    SELECT sales_agent,
            COUNT(pipeline.close_value) AS `total`,
            COUNT(pipeline.close_value)
            FILTER(WHERE pipeline.close_value > 1000) AS `over 1000`,
            AVG(sales_pipeline.close_value) AS `average of total`,
            AVG(sales_pipeline.close_value)
            FILTER(WHERE sales_pipeline.close_value > 1000) AS `average of over 1000`

    FROM sales_pipeline AS pipeline
    WHERE pipeline.deal_stage = "Won"
    GROUP BY pipeline.sales_agent
    HAVING COUNT(pipeline.close_value) FILTER (WHERE pipeline.close_vlue >1000) >100
    ORDER BY total

     

     


     

    댓글

Designed by Tistory.