SQL

[빅쿼리] countif()의 용법

suky_da 2024. 2. 7. 17:50

빅쿼리에서 사용하는 countif

COUNTIF(
  [ DISTINCT ]
  expression
)
[ OVER over_clause ]

over_clause:
  { named_window | ( [ window_specification ] ) }

window_specification:
  [ named_window ]
  [ PARTITION BY partition_expression [, ...] ]
  [ ORDER BY expression [ { ASC | DESC }  ] [, ...] ]
  [ window_frame_clause ]

 

1. countif(조건식) 의 형태로 사용

2. 조건식은 True, False 의 형태로 답이 나오는 불리언 식을 사용

 

사용 예시

SELECT day,
	COUNTIF(session_id IS NOT NULL) AS sessions
FROM table_1
GROUP BY day
ORDER BY day

 

=> 빅쿼리에서는 쓸 수 있지만, MySQL에선 쓸 수 없음

 

COUNTIF(session_id IS NOT NULL)
COUNT(session_id)

이 두 식은 같은 결과값을 가짐.
빅쿼리에서 
count(session_id) 의 경우 null 값이 아닌 sesseion_id의 수를 반환하게 됨. 

그럼 count를 두고 countif를 왜 써야 하는가? 
지금은 조건으로 not null이 들어갔기 때문에 동일한 값이 나오는 것
조건으로 x>3 이라거나 다른 조건이 들어간다면 
countif 의 쓸모가 있음.

 

 

어렵다~!


참고

https://cloud.google.com/bigquery/docs/reference/standard-sql/aggregate_functions#countif

 

Aggregate functions  |  BigQuery  |  Google Cloud

GoogleSQL for BigQuery supports the following general aggregate functions. To learn about the syntax for aggregate function calls, see Aggregate function calls. Function list Name Summary ANY_VALUE Gets an expression for some row. ARRAY_AGG Gets an array o

cloud.google.com