- CAST(column_name AS integer) = column_name::integer
<DATA FORMAT>
- 날짜형의 경우, 나라마다 저장 방식이 다르며, 문자형으로 저장하면 시간 순서대로 정렬되지 않는다. 보통 YYYY-MM-DD로 저장됨.
- INTERVAL로 날짜 간 산술이 가능하다.
<STRING FUNCTIONS>
> LEFT,RIGHT,(LEFT/RIGHT(string, number of characters), LENGTH, TRIM, POSITION(STROPS)
- TRIM
the beginning ('leading'), the end ('trailing'), or both ('both', as used above), trim using FROM .
- POSITION/ STROPS
자리 위치 알려줌
- SUBSTR
SUBSTR(*string*, *starting character position*, *# of characters*), 중간부터 자르고 싶다면혼합된 예시
- CONCAT
여러개의 열의 문자값을 합치고 싶을 때
concat=
- UPPER/LOWER
- strings to date
특정한 시간 형태만 추출할 때The DATE_TRUNC function rounds a date to whatever precision you specify. The value displayed is the first value in that period. So when you DATE_TRUNC by year, any value in that year will be listed as January 1st of that year
현재 시간 포함할 때, FROM없이 가능시간대 적용할 때 AT TIME ZONE
현재와 날짜 산술할 때
- COALESCE
null 값을 다른 값으로 대체할 때
<SUBQUERIES>
- 서브쿼리는 단계적으로 쿼리를 수행할 때 유용, 서브쿼리도 독립적으로 수행 (your inner query must actually run on its own, as the database will treat it as an independent query. Once the inner query runs, the outer query will runusing the results from the inner query as its underlying table)
sub는 서브쿼리의 별칭으로 사용
- Using subqueries to aggregate in multiple stages
먼저 월 사고 수를 구하고(서브쿼리), 월평균을 구함(바깥)
-Subqueries in conditional logic
왼쪽의 경우에는 하나의 셀만 결과값이 나오지만 IN 을 사용하면 여러개의 값이 나온다 conditioncal logic 쿼리에서는 별칭을 쓰지 않는다.
-Joining subqueries
위의 WHERE IN과 같은 결과 값을 가져온다
예시 1
예시 2
-Subqueries and UNIONs
한 데이터셋에서 파트가 나누어진 경우 서브쿼리에서 유니온을 이용해서 한꺼번에 결과 처리 가능
<WINDOW FUCTIONS>
- 상관있는 행을 교차하여 aggregation이 가능. 일반 aggregation 처럼 하나로 통합되는 것이 아니라 하나의 독립된 형태를 가짐.
- You can't use window functions and standard aggregations in the same query. More specifically, you can't include window functions in aGROUP BYclause.
Adding OVER designates it as a window function. You could read the above aggregation as "take the sum of duration_seconds over the entire result set, in order by start_time ."
-Basic windowing syntax
ORDER BY 가 없어도 같은 문장이다. 다만, PARTITION BY는 각 구분(partition)을 별개의 것으로 대한다. Scroll down until the start_terminal value changes and you will notice that running_total starts over. That's what happens when you group using PARTITION BY
- The usual suspects: SUM, COUNT, and AVG
동일한 내용
- ROW_NUMBER()
왼쪽과 오른쪽의 차이는 PARTITION BY의 경우 PARTITION마다 행개수가 1로 리셋된다.
- RANK() and DENSE_RANK()
- NTILE(%)
NTILE(*# of buckets*), ORDER BY(어떤 열의 percentile 구하는지)
- LAG and LEAD
LAG pulls from previous rows and LEAD pulls from following rows
The first row of the difference column is null because there is no previous row from which to pull. Similarly, using LEAD will create nulls at the end of the dataset. If you'd like to make the results a bit cleaner, you can wrap it in an outer query to remove nulls
- Defining a window alias
오른쪽이 더 정리된 것을 볼 수 있다.
<PIVOT>
원래 데이터 형태서브쿼리에 먼저 해당 내용을 넣는다. 여기서 ORDER BY가 사라진 이유는 어차피 outer query에서 정렬되기 때문이다.
그 다음 SELECT 안 CASE문으로 열을 생성해준다 total_players가 들어간 이유는 합계값으로 내림 차순 정렬을 하기 위해서이다.
가장 먼저 열의 내용을 행의 값으로 바꾼다.해당 쿼리로 밑의 좌 이미지 형태의 데이터에서 우 이미지 형태의 데이터로 전환